Error if number less than 10000 entered?

Hi, I have this line in my form script that gives an error message if they enter something less than 2 characters, but I would like it to give the error if they enter a number less than 10000. Can someone please let me know what I need to change it to? I would also like for it to work whether they use commas or not. Thanks!

if(strlen($pricefrom) < 2) {
$error_message .= ‘The Price From you entered does not appear to be valid.<br />’;
}

What values would you expect in your $pricefrom variable? Would it always be an integer?


if(intval($pricefrom) < 10000) {
$error_message .= 'The Price From you entered does not appear to be valid.<br />';
}

Though it would be smart to make that message a little more detailed when the condition evaluates to true. See the intval() documentation for that. There could be separate messages for an invalid integer, out of range and an integer less than 1000 rather than a single message that covers all those cases.

Thank you oddz, that works if they don’t use a comma, but gives the error if they do. Is it possible to make it work with or without commas? Thanks!

Use preg_replace to remove any unwanted characters. I’m assuming you just want to keep numbers and periods (but if you want to keep periods, you couldn’t treat the number as an int anymore)

$value = preg_replace('@\\[^0-9\\.]@', '', $value);

Thank you Force Flow, but where do I add this in at? I tried putting

$pricefrom = preg_replace('@\\[^0-9\\.]@', '', $pricefrom);

both above and below the 3 lines above and it didn’t work.

Whoops, I had a typo in the regex.

$pricefrom = preg_replace('@[^0-9\\.]@', '', $pricefrom);

It should go above the three lines before you do any operations with it.

Granted, since we don’t know what the rest of your code looks like, this approach probably isn’t the best way to do this. If any characters are non-number characters, you should kick the form back to the user with an error message next to the form field saying something like “This field only accepts numbers. Letters and symbols are not allowed”.

With this approach, if a user entered “99luftballoons”, you would end up with “99”. Or, if someone typed in l33t, you would get “33”. You will probably get input that you don’t want or don’t expect.