I have multiple fields in my form and the input can only be any number from: 0 to 20 I'm trying the following without any success function validate_qty($t) { $forbidden = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'); foreach ($forbidden as $p) if (strpos($t, $p) !== false) return true; return false; } Code (markup): with this elseif (!validate_qty($_POST['pr1'])) { echo "You can only enter a number (0 to 20) in the quantity field<br>"; echo "<a href='javascript:history.back(1);'>Click here to return</a>"; } elseif (!validate_qty($_POST['pr2'])) { echo "You can only enter a number (0 to 20) in the quantity field<br>"; echo "<a href='javascript:history.back(1);'>Click here to return</a>"; } Code (markup):
looks like you are doing a binary compare !== and declaring the array with string items. make sure you are comparing the correct datatype to datatype when using binary compare.
On the right track, but this will ensure a number between 0 and 20 and reject non-numeric input. function validate_qty($i) { if( preg_match( '![^0-9]!', $i) ) return false; if( $i >= 0 and $i <21) return true; return false; } Code (markup):