zip code validation/regular expression

From: Date: Mon, 02 Jun 2003 13:24:40 +0000
Subject: zip code validation/regular expression
Groups: php.general 
Request: Send a blank email to php-general+get-149790@lists.php.net to get a copy of this message
Simply trying to use regular expressions to validate a 5 or 5+4 zip code, excluding 00000 and 00000-0000. Looked on http://www.regexlib.com, and that's where I found the pattern below, but it doesn't work. The best I can do on my own is two separate regular expressions, one to match a valid zip, and another to match the 00000 invalid code. I would really like to condense it into one, just because I think it should be possible. This code below is suppose to do all this for me, but it's not matching as I'd hoped. I get results like this: 21832-1234 MATCHES. 00000-0000 MATCHES. 00000 MATCHES. 1234 doesn't match. ABCDEF doesn't match. ABC doesn't match. I'd appreciate any help. Thanks, jason <?php $regex1 = "/^(\?(^00000(|-0000))|(\d{5}(|-\d{4})))$/"; $zips = array("21243","21832-1234","00000-0000","00000","1234","ABCDEF","ABC"); foreach ($zips as $zip) { if (preg_match($regex1,$zip)) { echo "$zip MATCHES.<br>"; } else { echo "$zip doesn't match.<br>"; } } ?> _________________________________________________________________ MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. http://join.msn.com/?page=features/virus

« previous php.general (#149790) next »