Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression: Postal Code

Status
Not open for further replies.

diezy

Technical User
Sep 2, 2002
50
CA
hey, im trying to create a regular expression for canadian postal codes

i want to validate either one of these expressions


l7p3r3
l7p 3r3
l7p-3r3

L7P3R3
L7P 3R3
L7P-3R3


right now i have this as my expression
[a-zA-Z][0-9][a-zA-Z][-|\s|\S][0-9][a-zA-Z][0-9]

however there is a problem, and it seems to be where the 4th character is("[-|\s|\S]").where it can be either a dash("-") or a space(" ") or no space("")

if someone could please help out with the correct expression for this expression would be great.

thanks
 
What exactly is the problem?

This script:

Code:
<?php

$codes = array ('l7p3r3','l7p 3r3','l7p-3r3','L7P3R3','L7P 3R3','L7P-3R3', 'l7p - 3r3', 'l7p  3r3', 'l7p--3r3', 'l7p -3r3');

foreach ($codes as $code)
{
	print $code . ":";
	if (preg_match ('/^[a-z][0-9][a-z][- ]{0,1}[0-9][a-z][[0-9]$/i', $code) != 0)
		print 'good';
	else
		print 'bad';
	
	print "\n";
}
?>

Seems to accept or reject the appropriate strings.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
it was rejecting correct postal code and accepting that was my problem

but i see where i made my mistake now from your example..

thanks alot

diezy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top