user1084949
user1084949

Reputation: 65

preg_match to accept number between range

How I can validate a age feild in php with preg_match to only accept number between [18 - 60].

Here is the code I write to accept 2 digit only, but I want to specify a reange of number.

if(preg_match("/^\d{2}$/", $_POST["age"]) === 0)
      $errage = '<p class="errText">Age must between 18 -60 digits </p>';

Upvotes: 0

Views: 3200

Answers (1)

zerkms
zerkms

Reputation: 254896

You don't need regular expression at all:

if ($_POST["age"] < 18 || $_POST["age"] > 60) $errage = 'Age must between 18 -60 digits';

And just for the information - this is how regex would look like:

~^(1[89]|[2345]\d|60)$~

Upvotes: 4

Related Questions