• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

PHP simple comparison (validation) question

I have a web form input text field where i want the user to enter a 5 digit zipcode.
What is the best way (in php, not JS) to make sure it's 5 digits 0-9
I've tried  < and > comparisons, but characters evidently get converted to integers and some short strings can fall within the 0-99999 range.

thanks
0
willsherwood
Asked:
willsherwood
3 Solutions
 
glcumminsCommented:
There are a number of PHP ZIP code validation scripts available, which helps you to avoid reinventing the wheel. One common one is here:

http://www.hotscripts.com/Detailed/46060.html
0
 
hieloCommented:
assuming the name of your field is zipcode:
if( preg_match('/^\d\d\d\d\d([-]\d\d\d)?$/', $_REQUEST['zipcode']) )
{
 $zip = $_REQUEST['zipcode'];
 echo "$zip  is valid";
}
0
 
hernst42Commented:
try to validate:

if (!preg_match('/^\d{5}$/', $zipcodeToValidate) {
 // invalid zip code
}
0
 
willsherwoodAuthor Commented:
thanks to you all.  (simultaneously)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now