Forum Moderators: coopster
I've got a form on my bosses' Web site where a customer can enter a price range and get a list of our "stuff" that falls within that price range. I'd like to set something up so that if they enter letters or commas or anything other than numerals in the price range form fields, they'll be gently encouraged to go back and do it right. :)
This is what I've got for the time being:
if (!eregi('^[0-9]', $pricemin) ¦¦!eregi('^[0-9]', $pricemax)) {
echo '<P>You have not entered a valid price range. ';
echo 'Be sure to enter only numbers, and no letters or punctuation marks, when searching for a price range. ';
echo 'Click the Back button on your browser to search again.';
exit;
}
This works, insofar as if someone types "blah" in the pricemin or the pricemax form field, they'll get the message. But if some well meaning person types "50,000" or "$50,000" instead of "50000" in there, they currently get a MySQL error. I've got a note telling people not to use letters or punctuation, but you know how people are.
All the tutorials I've seen talk about including or excluding different characters based on different criteria, but everything I've tried to put a comma in there has failed and/or messed up the query somehow. What am I missing?
Your regular expression is merely checking for a digit in the first position of both entries. If you are only wanting digits and that's it, you need to modify the regular expression to check the entire string for only digits.
if (!preg_match [php.net]('/[[:^digit:]]/', $pricemin) ¦¦ (!preg_match('/[[:^digit:]]/', $pricemax)) {The forum breaks the pipe character (¦) so if you cut and paste this code, don't forget to rekey the pipes.
Also, you could get quite a bit more forgiving by stripping any dollar signs and commas ( see str_replace() [php.net] ) And if you are concerned about decimal points, you can chop that off as well. Just some ideas...