Forum Moderators: coopster
if($num >= 0 && $num <= 20)...
<?php
$pattern = "/^([0-9]¦1[0-9]¦20)$/";
$num = 0;
while ($num < 100) {
if (preg_match($pattern, $num) ) {
echo "$num was a value 0-20<br/>\n";
} else {
echo "$num was not a value 0-20<br/>\n";
}
$num++;
}
?>
--Nick
if you can allow things like '00' or '09' then this one will work as well:
$pattern = "/^([01]?[0-9]¦20)$/";
it doesn't really matter if the number is outside 20, I just don't understand why it throws an error if I use {1,2} instead of {0,2} for the length?
I should say that it's to check the value of the numerical captcha I've built (same type of what's on Matt Cutts page). I just want to make sure it's a number of no more that 2 digits.
It's not an error per se (i.e not a php error message), but the 'captcha' is supposed to be validated and block form submission until both numbers are the same. When I use the {1,2} and leave the captcha form field empty, the form goes through. When I use the {0,2} it behaves as it should.
'/^[0-9]{0,2}$/i'
Go to the beginning of the string, allow a digit 0-9 of at least 0 length and at most a length of 2 (so that means NULL - 99 will work). Then go to the end of the string........ then use case-insensitive for whatever reason. Why is that in there? Can you do a case-insensitive search with numbers? That's not sarcasm, I'm genuinely interested.
--Nick