Solved

PHP  simple comparison (validation) question

Posted on 2008-06-18
4
220 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

13 Experts available now in Live!

Get 1:1 Help Now