Solved

PHP  simple comparison (validation) question

Posted on 2008-06-18
4
222 Views
Last Modified: 2010-04-21
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
Comment
Question by:willsherwood
4 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 167 total points
ID: 21814616
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
 
LVL 82

Assisted Solution

by:hielo
hielo earned 167 total points
ID: 21814619
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
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 166 total points
ID: 21814627
try to validate:

if (!preg_match('/^\d{5}$/', $zipcodeToValidate) {
 // invalid zip code
}
0
 

Author Closing Comment

by:willsherwood
ID: 31468445
thanks to you all.  (simultaneously)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now