Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #10587 > unrolled thread
| Started by | Twayne <nobody@spamcop.net> |
|---|---|
| First post | 2013-08-20 13:27 -0400 |
| Last post | 2013-08-31 20:07 -0400 |
| Articles | 20 on this page of 34 — 13 participants |
Back to article view | Back to comp.lang.php
Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 13:27 -0400
Re: Zip Codes ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-20 11:59 -0600
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:01 -0400
Re: Zip Codes ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 13:08 -0500
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:03 -0400
Re: Zip Codes ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 14:36 -0500
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 20:17 -0400
Re: Zip Codes ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 15:06 -0400
Re: Zip Codes ctype? Pregmatch? RESOLVED Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:53 -0400
Re: Zip Codes ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 20:29 -0400
Re: Zip Codes ctype? Pregmatch? RESOLVED Moon Elf <moonelf@moonelfsystem.net> - 2013-12-16 10:02 +0000
Re: Zip Codes ctype? Pregmatch? RESOLVED Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-16 12:27 +0000
Re: Zip Codes ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:52 -0400
Re: Zip Codes ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:53 +0200
Re: Zip Codes ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:58 +0200
Re: Zip Codes ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 15:06 +0200
Re: Zip Codes ctype? Pregmatch? Curtis Dyer <dyer85@gmail.com> - 2013-08-24 10:22 +0000
Re: Zip Codes ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-24 12:52 +0200
Re: Zip Codes ctype? Pregmatch? Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-08-25 14:31 +0000
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:21 -0400
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:17 -0400
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 19:10 -0400
Re: Zip Codes ctype? Pregmatch? BootNic <bootnic.bounce@gmail.com> - 2013-08-21 22:09 -0400
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-22 18:22 -0400
Re: Zip Codes ctype? Pregmatch? Jeff North <jnorthau@yahoo.com.au> - 2013-08-21 21:25 +1000
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 18:53 -0400
Re: Zip Codes ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-21 22:46 -0400
Re: Zip Codes ctype? Pregmatch? gordonb.qnlcm@burditt.org (Gordon Burditt) - 2013-08-31 01:17 -0500
Re: Zip Codes ctype? Pregmatch? Fiver <numeralfive@gmail.com> - 2013-08-31 16:40 +0200
Re: Zip Codes ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-31 11:56 -0400
Re: Zip Codes ctype? Pregmatch? Welsh Vanner <welshvanners@welsh-vanners.co.uk> - 2013-08-31 20:23 +0100
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:21 -0400
Re: Zip Codes ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-31 09:56 -0600
Re: Zip Codes ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:07 -0400
Page 1 of 2 [1] 2 Next page →
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 13:27 -0400 |
| Subject | Zip Codes ctype? Pregmatch? |
| Message-ID | <kv08v0$614$1@speranza.aioe.org> |
Hi all,
I'm attempting to check for US and Canadian zip codes (postal codes).
The US is easy; mostly just be sure it's five numerics and except
"00000" and "99999". But Canadian is a different story because:
It consists of alternating alpha and numeric characters (AnAnAn) but
not the entire alphabet. 8 N.A. English letters are not used, as in
DFIOQUW AND Z or put another way, they only use 18 letters in their
postal codes.
I haven't see a single example in all my research to check if the
1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th
characters are numeric.
I've tried preg_match and strpos without succees, likely due to my own
weakness with preg_match, and regex creates an incredibly long statement
I'm sure it's not right to put upon the servers; they slow down even my
local server XAMPP & PHP 5.3 on win 7.
Might anyone have a better method?
Or know of any functions anywhere that could be modified to be used?
[toc] | [next] | [standalone]
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2013-08-20 11:59 -0600 |
| Message-ID | <kv0apm$ml0$1@dont-email.me> |
| In reply to | #10587 |
Twayne wrote:
> Hi all,
>
> I'm attempting to check for US and Canadian zip codes (postal codes).
> The US is easy; mostly just be sure it's five numerics and except
> "00000" and "99999". But Canadian is a different story because:
> It consists of alternating alpha and numeric characters (AnAnAn) but
> not the entire alphabet. 8 N.A. English letters are not used, as in
> DFIOQUW AND Z or put another way, they only use 18 letters in their
> postal codes.
Note that postcodes must have a space in
the middle, ie, AnA nAn. In practice,
however, there is so much brain-dead
software in use that believes otherwise, it
would be prudent to make the space optional.
From the Canada Post Addressing Guide:
"Postal codes must be printed in
upper case with the first three
elements separated from the last
three by one space (no hyphens)."
--
Regards,
Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 15:01 -0400 |
| Message-ID | <kv0een$mdt$1@speranza.aioe.org> |
| In reply to | #10588 |
On 2013-08-20 1:59 PM, Martin Leese wrote: ... > > From the Canada Post Addressing Guide: > "Postal codes must be printed in > upper case with the first three > elements separated from the last > three by one space (no hyphens)." > OUCH! Thanks! After more research I found a trail about that; forgot all the details but the space it seems is something special more than just esthetics. Thanks much!
[toc] | [prev] | [next] | [standalone]
| From | Robert Heller <heller@deepsoft.com> |
|---|---|
| Date | 2013-08-20 13:08 -0500 |
| Message-ID | <EuKdnWbC4P4PLY7PnZ2dnUVZ_uadnZ2d@giganews.com> |
| In reply to | #10587 |
At Tue, 20 Aug 2013 13:27:50 -0400 Twayne <nobody@spamcop.net> wrote:
>
> Hi all,
>
> I'm attempting to check for US and Canadian zip codes (postal codes).
> The US is easy; mostly just be sure it's five numerics and except
> "00000" and "99999".
5+4:
nnnnn-mmmm
Most of the time, just the basic 5 digits is enough, but sometimes the USPS
wants the additional 4 digits as well.
Whether or not you include the extra 4 digits or not depends on what you are
using the zip code for. UPS and FexEX for example don't use the extra 4
digits, but the USPS does. The extra four digits are important mostly for big
city addresses, where there might be multiple branch POs and/or delivery
routes, etc. for a given post office.
--
Robert Heller -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 15:03 -0400 |
| Message-ID | <kv0eih$mdt$2@speranza.aioe.org> |
| In reply to | #10589 |
On 2013-08-20 2:08 PM, Robert Heller wrote: > At Tue, 20 Aug 2013 13:27:50 -0400 Twayne <nobody@spamcop.net> wrote: > >> >> Hi all, >> >> I'm attempting to check for US and Canadian zip codes (postal codes). >> The US is easy; mostly just be sure it's five numerics and except >> "00000" and "99999". > > 5+4: > > nnnnn-mmmm > > Most of the time, just the basic 5 digits is enough, but sometimes the USPS > wants the additional 4 digits as well. Agreed; but in this case it's only to ID a country. > > Whether or not you include the extra 4 digits or not depends on what you are > using the zip code for. UPS and FexEX for example don't use the extra 4 > digits, but the USPS does. The extra four digits are important mostly for big > city addresses, where there might be multiple branch POs and/or delivery > routes, etc. for a given post office. > > Thanks, Twayne`
[toc] | [prev] | [next] | [standalone]
| From | Robert Heller <heller@deepsoft.com> |
|---|---|
| Date | 2013-08-20 14:36 -0500 |
| Message-ID | <7_udnZBMJ-vcWI7PnZ2dnUVZ_s2dnZ2d@giganews.com> |
| In reply to | #10592 |
At Tue, 20 Aug 2013 15:03:48 -0400 Twayne <nobody@spamcop.net> wrote:
>
> On 2013-08-20 2:08 PM, Robert Heller wrote:
> > At Tue, 20 Aug 2013 13:27:50 -0400 Twayne <nobody@spamcop.net> wrote:
> >
> >>
> >> Hi all,
> >>
> >> I'm attempting to check for US and Canadian zip codes (postal codes).
> >> The US is easy; mostly just be sure it's five numerics and except
> >> "00000" and "99999".
> >
> > 5+4:
> >
> > nnnnn-mmmm
> >
> > Most of the time, just the basic 5 digits is enough, but sometimes the USPS
> > wants the additional 4 digits as well.
>
> Agreed; but in this case it's only to ID a country.
You might need to 'accept' the extra 4 digits, since people are going to enter
them and will be mifted if your page rejects it. Just quietly drop the extra
digits.
> >
> > Whether or not you include the extra 4 digits or not depends on what you are
> > using the zip code for. UPS and FexEX for example don't use the extra 4
> > digits, but the USPS does. The extra four digits are important mostly for big
> > city addresses, where there might be multiple branch POs and/or delivery
> > routes, etc. for a given post office.
> >
> >
> Thanks,
>
> Twayne`
>
--
Robert Heller -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 20:17 -0400 |
| Message-ID | <kv10uo$73l$1@speranza.aioe.org> |
| In reply to | #10594 |
On 2013-08-20 3:36 PM, Robert Heller wrote:
> At Tue, 20 Aug 2013 15:03:48 -0400 Twayne <nobody@spamcop.net> wrote:
>
>>
>> On 2013-08-20 2:08 PM, Robert Heller wrote:
>>> At Tue, 20 Aug 2013 13:27:50 -0400 Twayne <nobody@spamcop.net> wrote:
>>>
>>>>
>>>> Hi all,
>>>>
>>>> I'm attempting to check for US and Canadian zip codes (postal codes).
>>>> The US is easy; mostly just be sure it's five numerics and except
>>>> "00000" and "99999".
>>>
>>> 5+4:
>>>
>>> nnnnn-mmmm
>>>
>>> Most of the time, just the basic 5 digits is enough, but sometimes the USPS
>>> wants the additional 4 digits as well.
>>
>> Agreed; but in this case it's only to ID a country.
>
> You might need to 'accept' the extra 4 digits, since people are going to enter
> them and will be mifted if your page rejects it. Just quietly drop the extra
> digits.
>
>>>
>>> Whether or not you include the extra 4 digits or not depends on what you are
>>> using the zip code for. UPS and FexEX for example don't use the extra 4
>>> digits, but the USPS does. The extra four digits are important mostly for big
>>> city addresses, where there might be multiple branch POs and/or delivery
>>> routes, etc. for a given post office.
>>>
>>>
>> Thanks,
>>
>> Twayne`
>>
>
I hear you, but when I specifically ask for a 5-digit US Zip Code if the
person wants to insist on 9, that's not the kind of person I want to
hear from anyway.
Besides, a 9-digit code is a lot more personal information than is
needed and very few people want to give away more information about
themselves than is necessary. A 9 digit zip code can in many cases lead
you right to a person's street address. With a name and an address, it's
only a short step to their ss# being compromised and then the field is
set for full identity theft.
Regards,
Twayne`
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 15:06 -0400 |
| Subject | Re: Zip Codes ctype? Pregmatch? RESOLVED |
| Message-ID | <kv0emr$mdt$3@speranza.aioe.org> |
| In reply to | #10587 |
As usual, after futzing with a problem for a couple days and end up finally finding multiple solutions. Thanks all, Twayne` On 2013-08-20 1:27 PM, Twayne wrote: > Hi all, > > I'm attempting to check for US and Canadian zip codes (postal codes). > The US is easy; mostly just be sure it's five numerics and except > "00000" and "99999". But Canadian is a different story because: > It consists of alternating alpha and numeric characters (AnAnAn) but > not the entire alphabet. 8 N.A. English letters are not used, as in > DFIOQUW AND Z or put another way, they only use 18 letters in their > postal codes. > I haven't see a single example in all my research to check if the > 1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th > characters are numeric. > > I've tried preg_match and strpos without succees, likely due to my own > weakness with preg_match, and regex creates an incredibly long statement > I'm sure it's not right to put upon the servers; they slow down even my > local server XAMPP & PHP 5.3 on win 7. > > Might anyone have a better method? > > Or know of any functions anywhere that could be modified to be used? >
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2013-08-20 19:53 -0400 |
| Subject | Re: Zip Codes ctype? Pregmatch? RESOLVED |
| Message-ID | <kv0vi0$g9t$2@dont-email.me> |
| In reply to | #10593 |
On 08/20/2013 03:06 PM, Twayne wrote: > As usual, after futzing with a problem for a couple days and end up > finally finding multiple solutions. > > Thanks all, > > Twayne` > > > On 2013-08-20 1:27 PM, Twayne wrote: >> Hi all, >> >> I'm attempting to check for US and Canadian zip codes (postal codes). >> The US is easy; mostly just be sure it's five numerics and except >> "00000" and "99999". But Canadian is a different story because: >> It consists of alternating alpha and numeric characters (AnAnAn) but >> not the entire alphabet. 8 N.A. English letters are not used, as in >> DFIOQUW AND Z or put another way, they only use 18 letters in their >> postal codes. >> I haven't see a single example in all my research to check if the >> 1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th >> characters are numeric. >> >> I've tried preg_match and strpos without succees, likely due to my own >> weakness with preg_match, and regex creates an incredibly long statement >> I'm sure it's not right to put upon the servers; they slow down even my >> local server XAMPP & PHP 5.3 on win 7. >> >> Might anyone have a better method? >> >> Or know of any functions anywhere that could be modified to be used? >> > I responded even though you SOLVED... at least let us know what your solution was. -- Norman Registered Linux user #461062 -Have you been to www.php.net yet?-
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-20 20:29 -0400 |
| Subject | Re: Zip Codes ctype? Pregmatch? RESOLVED |
| Message-ID | <kv11lq$8eb$1@speranza.aioe.org> |
| In reply to | #10597 |
On 2013-08-20 7:53 PM, Norman Peelman wrote:
> On 08/20/2013 03:06 PM, Twayne wrote:
...
>>
>
> I responded even though you SOLVED... at least let us know what your
> solution was.
>
Oh, sorry; guess I was in a hurry. I simply came across a couple of
methods, neither of which was properly coded, and use a combo of the two
methods to assemble mine. If you're interested, here's a crimp sheet I
collected:
--------------
From the Canada Post Addressing Guide:
"Postal codes must be printed in
upper case with the first three
elements separated from the last
three by one space (no hyphens)."
=================================================
POSTAL CODES FOR 12 COUNTRIES
<?php
$country_code="US";
$zip_postal="11111";
$ZIPREG=array(
"US"=>"^\d{5}([\-]?\d{4})?$",
"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
"DE"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
"CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\
{0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
"FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
"IT"=>"^(V-|I-)?[0-9]{5}$",
"AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
"NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
"ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
"SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
"BE"=>"^[1-9]{1}[0-9]{3}$"
);
if ($ZIPREG[$country_code]) {
if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){
//Validation failed, provided zip/postal code is not valid.
} else {
//Validation passed, provided zip/postal code is valid.
}
} else {
//Validation not available
}
=======================================================================
OR ...
function fnValidatePostal($mValue, $sRegion = '')
{
$mValue = strtolower($mValue));
$sFirst = substr($mValue, 0, 1);
$sRegion = strtolower($sRegion);
$aRegion = array(
'nl' => 'a',
'ns' => 'b',
'pe' => 'c',
'nb' => 'e',
'qc' => array('g', 'h', 'j'),
'on' => array('k', 'l', 'm', 'n', 'p'),
'mb' => 'r',
'sk' => 's',
'ab' => 't',
'bc' => 'v',
'nt' => 'x',
'nu' => 'x',
'yt' => 'y'
);
if (preg_match('/[abceghjlkmnprstvxy]/', $sFirst) &&
!preg_match('/[dfioqu]/', $mValue) && preg_match('/^\w\d\w[-
]?\d\w\d$/', $mValue))
{
if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
{
if (is_array($aRegion[$sRegion]) && in_array($sFirst,
$aRegion[$sRegion]))
{
return true;
}
else if (is_string($aRegion[$sRegion]) && $sFirst ==
$aRegion[$sRegion])
{
return true;
}
}
else if (empty($sRegion))
{
return true;
}
}
return false;
}
===================================================
AND
===========================================================
Sounds like a regexp pattern like:
Code:
/^(?:[A-CEGHJ-NPR-TVX][0-9]){3}$/
could be used to validate the form of a given Canadian postal code
based on the description you gave. (Whether or not the postal code is
truly valid/used is, of course, another matter altogether.)
All that being said, I see that Canada Post has an API (and I'm
fairly sure the USPS does, too) ... you might actually check validity
with the code issuing authority at the time of submission....
------------------------
I'm most interested in using the API's that are available though as when
I figure out how to access them programatically I'll do so. Most of the
places I want to validate postal codes turn out to have an online API,
seriously relieving me of a lot of code and nullifying the possibility
of future changes, although there apparently have been few in the last
decade or so.
I'm a neophyte with little experience in these matters yet.
Cheers,
Twayne`
[toc] | [prev] | [next] | [standalone]
| From | Moon Elf <moonelf@moonelfsystem.net> |
|---|---|
| Date | 2013-12-16 10:02 +0000 |
| Subject | Re: Zip Codes ctype? Pregmatch? RESOLVED |
| Message-ID | <slrnlatjrr.dlu.moonelf@sigtrans.org> |
| In reply to | #10599 |
On 2013-08-21, Twayne <nobody@spamcop.net> wrote:
> On 2013-08-20 7:53 PM, Norman Peelman wrote:
>> On 08/20/2013 03:06 PM, Twayne wrote:
>
> ...
>
>>>
>>
>> I responded even though you SOLVED... at least let us know what your
>> solution was.
>>
>
> Oh, sorry; guess I was in a hurry. I simply came across a couple of
> methods, neither of which was properly coded, and use a combo of the two
> methods to assemble mine. If you're interested, here's a crimp sheet I
> collected:
> --------------
> From the Canada Post Addressing Guide:
> "Postal codes must be printed in
> upper case with the first three
> elements separated from the last
> three by one space (no hyphens)."
>
>=================================================
>
>
> POSTAL CODES FOR 12 COUNTRIES
>
><?php
> $country_code="US";
> $zip_postal="11111";
>
> $ZIPREG=array(
> "US"=>"^\d{5}([\-]?\d{4})?$",
> "UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
> "DE"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
> "CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\
> {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
> "FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
> "IT"=>"^(V-|I-)?[0-9]{5}$",
> "AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
> "NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
> "ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
> "DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
> "SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
> "BE"=>"^[1-9]{1}[0-9]{3}$"
> );
>
A faster algorithm would be to use regexes which use .+ .* with a unique
fingerprint. The above code is grinding your system probably.
I am sure tutorials such as Mastering Regular Expressions 2nd ed. would help
out.
> if ($ZIPREG[$country_code]) {
>
> if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){
> //Validation failed, provided zip/postal code is not valid.
> } else {
> //Validation passed, provided zip/postal code is valid.
> }
>
> } else {
>
> //Validation not available
>
> }
>
>=======================================================================
> OR ...
>
>
>
> function fnValidatePostal($mValue, $sRegion = '')
> {
> $mValue = strtolower($mValue));
> $sFirst = substr($mValue, 0, 1);
> $sRegion = strtolower($sRegion);
>
> $aRegion = array(
> 'nl' => 'a',
> 'ns' => 'b',
> 'pe' => 'c',
> 'nb' => 'e',
> 'qc' => array('g', 'h', 'j'),
> 'on' => array('k', 'l', 'm', 'n', 'p'),
> 'mb' => 'r',
> 'sk' => 's',
> 'ab' => 't',
> 'bc' => 'v',
> 'nt' => 'x',
> 'nu' => 'x',
> 'yt' => 'y'
> );
>
> if (preg_match('/[abceghjlkmnprstvxy]/', $sFirst) &&
> !preg_match('/[dfioqu]/', $mValue) && preg_match('/^\w\d\w[-
> ]?\d\w\d$/', $mValue))
> {
> if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
> {
> if (is_array($aRegion[$sRegion]) && in_array($sFirst,
> $aRegion[$sRegion]))
> {
> return true;
> }
> else if (is_string($aRegion[$sRegion]) && $sFirst ==
> $aRegion[$sRegion])
> {
> return true;
> }
> }
> else if (empty($sRegion))
> {
> return true;
> }
> }
>
> return false;
> }
Usually you do not test on vars unless you don't know at runtime what they
are which is not good coding practice.
>===================================================
> AND
>
>===========================================================
>
> Sounds like a regexp pattern like:
>
> Code:
>
> /^(?:[A-CEGHJ-NPR-TVX][0-9]){3}$/
>
> could be used to validate the form of a given Canadian postal code
> based on the description you gave. (Whether or not the postal code is
> truly valid/used is, of course, another matter altogether.)
>
>
> All that being said, I see that Canada Post has an API (and I'm
> fairly sure the USPS does, too) ... you might actually check validity
> with the code issuing authority at the time of submission....
> ------------------------
>
>
>
> I'm most interested in using the API's that are available though as when
> I figure out how to access them programatically I'll do so. Most of the
> places I want to validate postal codes turn out to have an online API,
> seriously relieving me of a lot of code and nullifying the possibility
> of future changes, although there apparently have been few in the last
> decade or so.
> I'm a neophyte with little experience in these matters yet.
>
> Cheers,
>
> Twayne`
PHP is procedural if you want, which I like but you need to be clever in what
you write, procedurally or not.
ME
--
Member of the DR rogue circle.
Search and you will find.
[toc] | [prev] | [next] | [standalone]
| From | Doug Miller <doug_at_milmac_dot_com@example.com> |
|---|---|
| Date | 2013-12-16 12:27 +0000 |
| Subject | Re: Zip Codes ctype? Pregmatch? RESOLVED |
| Message-ID | <XnsA2984BDD8AB68dougmilmaccom@78.46.70.116> |
| In reply to | #12286 |
Moon Elf <moonelf@moonelfsystem.net> wrote in news:slrnlatjrr.dlu.moonelf@sigtrans.org:
> On 2013-08-21, Twayne <nobody@spamcop.net> wrote:
[...]
>><?php
>> $country_code="US";
>> $zip_postal="11111";
>>
>> $ZIPREG=array(
>> "US"=>"^\d{5}([\-]?\d{4})?$",
[...]
>
> A faster algorithm would be to use regexes which use .+ .* with a unique
> fingerprint. The above code is grinding your system probably.
>
> I am sure tutorials such as Mastering Regular Expressions 2nd ed. would help
> out.
Never mind that -- the bigger problem is that it's just plain wrong. Comparing to a RegEx can
determine only if a postal code has the correct *format*, not if it is actually a valid code.
According to this ,
>
>> if ($ZIPREG[$country_code]) {
>>
>> if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){
>> //Validation failed, provided zip/postal code is not valid.
>> } else {
>> //Validation passed, provided zip/postal code is valid.
>> }
00000-0000 is a "valid" US postal code. It's not. It's correctly *formatted*, but its contents do
not correspond to a valid ZIP+4.
99999 also fails; it matches the RegEx, but is not a valid ZIP code. Same problem with
11111, 22222, 33333, and 54321 -- and thousands of others. Out of the 100,000 possible 5-
digit zip codes, less than 42,000 are actually in use, but this algorithm will say that all 100,000
of them are valid.
And since only 42K out of 100K 5-digit zip codes are actually in use, *at most* 420 million of
the one billion possible ZIP+4 codes can be valid, making *at least* 580 million *more*
invalid ZIP+4 codes that this algorithm will incorrectly declare to be valid.
It's pretty likely that similar problems exist for the other 11 nations as well.
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2013-08-20 19:52 -0400 |
| Message-ID | <kv0vfd$g9t$1@dont-email.me> |
| In reply to | #10587 |
On 08/20/2013 01:27 PM, Twayne wrote:
> Hi all,
>
> I'm attempting to check for US and Canadian zip codes (postal codes).
> The US is easy; mostly just be sure it's five numerics and except
> "00000" and "99999". But Canadian is a different story because:
> It consists of alternating alpha and numeric characters (AnAnAn) but
> not the entire alphabet. 8 N.A. English letters are not used, as in
> DFIOQUW AND Z or put another way, they only use 18 letters in their
> postal codes.
> I haven't see a single example in all my research to check if the
> 1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th
> characters are numeric.
>
> I've tried preg_match and strpos without succees, likely due to my own
> weakness with preg_match, and regex creates an incredibly long statement
> I'm sure it's not right to put upon the servers; they slow down even my
> local server XAMPP & PHP 5.3 on win 7.
>
> Might anyone have a better method?
>
> Or know of any functions anywhere that could be modified to be used?
>
US Zip code:
[0-9]{5}(-{0,1}[0-9]{4}){0,1}
Canadian zip code (all one line, don't miss the space!):
([A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}
{1}([0-9]{1}[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-21 14:53 +0200 |
| Message-ID | <2791655.Q3g5IOY8jz@PointedEars.de> |
| In reply to | #10596 |
Norman Peelman wrote:
> On 08/20/2013 01:27 PM, Twayne wrote:
>> I'm attempting to check for US and Canadian zip codes (postal codes).
>> The US is easy; mostly just be sure it's five numerics and except
>> "00000" and "99999". But Canadian is a different story because:
>> It consists of alternating alpha and numeric characters (AnAnAn) but
>> not the entire alphabet. 8 N.A. English letters are not used, as in
>> DFIOQUW AND Z or put another way, they only use 18 letters in their
>> postal codes.
>> I haven't see a single example in all my research to check if the
>> 1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th
>> characters are numeric.
>>
>> I've tried preg_match and strpos without succees, likely due to my own
>> weakness with preg_match, and regex creates an incredibly long statement
>> I'm sure it's not right to put upon the servers; they slow down even my
>> local server XAMPP & PHP 5.3 on win 7.
>>
>> Might anyone have a better method?
>>
>> Or know of any functions anywhere that could be modified to be used?
>
> US Zip code:
> [0-9]{5}(-{0,1}[0-9]{4}){0,1}
^^^^^ ^^^^^^^^^^ ^^^^^
In Perl-Compatible Regular Expressions (PCRE), as also used by PHP's preg_*
functions, the following shorthands are available:
- “*” for “{0,}”
- “?” for “{0,1}”
- “+” for “{1,}”
- “\d” for “[0-9]” (includes more numeric characters in “UTF-8 mode”)
Thus, the above expression can be simplified to
\d{5}(-?\d{4})?
However, the specification above says that “00000” and “99999” are _not_
valid U.S. ZIP codes, so to be exact you cannot just use either “[0-9]{5}”
or “\d{5}”; but you would have to use, for example, a zero-width negative
lookahead:
$possibleZips = array('00000', '00001', '99998', '99999');
foreach ($possibleZips as $possibleZip)
{
preg_match('^(?![09]{5})\\d{5}(?:-?\\d{4})?$', $possibleZip, $matches);
var_dump($possibleZip);
var_dump($matches);
}
(thanks to Anubhava: <http://stackoverflow.com/a/9609624/855543>)
> Canadian zip code (all one line, don't miss the space!):
> ([A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}
^ ^^^^^^^^
> {1}([0-9]{1}[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})
“{1}” is superfluous in all regular expression flavours (in BRE the escaped
variant is superfluous). An expression that matches, matches exactly one
time unless a following quantifier says otherwise.
In a character class expression, ranges are _not_ delimited by comma.
A comma there is a *literal* comma instead (just like most other special
characters lose, and “-” gains meaning), and repetitions are ignored:
[A-C,E,G-H,J-N,P,R-T,V,X,Y]
matches the same strings as
[A-CEG-HJ-NPR-TVXY,]
So unless you want to allow commas in ZIP codes, you need to remove them
from the respective character class.
Thus, the above expression would have to be changed, and can be simplified
to
^(?:[A-CEG-HJ-NPR-TVXY]\d){3}$
(The “^” makes sure that the second, fourth, aso. character must be a digit.
Let \s* follow it if you want to allow leading whitespace. Likewise for “$”
and trailing whitespace.)
Anyhow, if an expression is repeated, and this repetition cannot be handled
with a quantifier like above, in programming languages like PHP that allow
this, code is easier readable if you assign the repeated expression to a
variable, and have the variable reference expanded:
$cdn_letter = '[A-CEG-HJ-NPR-TVXY]';
$pattern = "^{$cdn_letter}\\d{$cdn_letter}\\d{$cdn_letter}\\d\$";
[In certain programming languages, libraries like my JSX:regexp.js [1] are
useful that allow you to define and use your own character class escape
sequences, eliminating the need for variable expansion: "\\p{cdnLetter}".]
Note that expansion/repetition is semantically different from expression
backreferences:
$pattern2 = "([A-CEG-HJ-NPR-TVXY])\\d\\1\\d\\1\\d";
$pattern would match "A1B2C3"; $pattern2 would match "A1A2A3", but not
"A1B2C3".
PointedEars
___________
[1] <http://PointedEars.de/scripts/test/regexp> p.
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-21 14:58 +0200 |
| Message-ID | <7964092.hUFTFP9gJ2@PointedEars.de> |
| In reply to | #10601 |
Thomas 'PointedEars' Lahn wrote:
> Norman Peelman wrote:
>> On 08/20/2013 01:27 PM, Twayne wrote:
>>> I'm attempting to check for US and Canadian zip codes (postal codes).
>>> The US is easy; mostly just be sure it's five numerics and except
>>> "00000" and "99999". […]
>>
>> US Zip code:
>> [0-9]{5}(-{0,1}[0-9]{4}){0,1}
> ^^^^^ ^^^^^^^^^^ ^^^^^
> In Perl-Compatible Regular Expressions (PCRE), as also used by PHP's
> preg_* functions, the following shorthands are available:
>
> - “*” for “{0,}”
> - “?” for “{0,1}”
> - “+” for “{1,}”
> - “\d” for “[0-9]” (includes more numeric characters in “UTF-8 mode”)
>
> Thus, the above expression can be simplified to
>
> \d{5}(-?\d{4})?
>
> However, the specification above says that “00000” and “99999” are _not_
> valid U.S. ZIP codes, so to be exact you cannot just use either “[0-9]{5}”
> or “\d{5}”; but you would have to use, for example, a zero-width negative
> lookahead:
>
> $possibleZips = array('00000', '00001', '99998', '99999');
> foreach ($possibleZips as $possibleZip)
> {
> preg_match('^(?![09]{5})\\d{5}(?:-?\\d{4})?$', $possibleZip,
Replace this line with
preg_match('/^(?![09]{5})\\d{5}(?:-?\\d{4})?$/', $possibleZip,
(add the delimiter).
> $matches);
> var_dump($possibleZip);
> var_dump($matches);
> }
>
> (thanks to Anubhava: <http://stackoverflow.com/a/9609624/855543>)
--
PointedEars
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-21 15:06 +0200 |
| Message-ID | <9308699.c6ZVUcr4Hi@PointedEars.de> |
| In reply to | #10602 |
Thomas 'PointedEars' Lahn wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Norman Peelman wrote:
>>> On 08/20/2013 01:27 PM, Twayne wrote:
>>>> I'm attempting to check for US and Canadian zip codes (postal codes).
>>>> The US is easy; mostly just be sure it's five numerics and except
>>>> "00000" and "99999". […]
>>>
>>> US Zip code:
>>> [0-9]{5}(-{0,1}[0-9]{4}){0,1}
>> ^^^^^ ^^^^^^^^^^ ^^^^^
>> […]
>> However, the specification above says that “00000” and “99999” are _not_
>> valid U.S. ZIP codes, so to be exact you cannot just use either
>> “[0-9]{5}” or “\d{5}”; but you would have to use, for example, a
>> zero-width negative lookahead:
>>
>> $possibleZips = array('00000', '00001', '99998', '99999');
$possibleZips = array('00000', '00001', '99998', '90909', '99999');
>> foreach ($possibleZips as $possibleZip)
>> {
>> preg_match('^(?![09]{5})\\d{5}(?:-?\\d{4})?$', $possibleZip,
>
> Replace this line with
>
> preg_match('/^(?![09]{5})\\d{5}(?:-?\\d{4})?$/', $possibleZip,
>
> (add the delimiter).
Which is still not correct, because '/[09]{5}/' matches "90909", which is
thus designated not valid. ISTM that alternation is required here:
preg_match('/^(?!0{5}|9{5})\\d{5}(?:-?\\d{4})?$/', $possibleZip);
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
[toc] | [prev] | [next] | [standalone]
| From | Curtis Dyer <dyer85@gmail.com> |
|---|---|
| Date | 2013-08-24 10:22 +0000 |
| Message-ID | <kva1hr$rc0$1@dont-email.me> |
| In reply to | #10603 |
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
> Thomas 'PointedEars' Lahn wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Norman Peelman wrote:
>>>> On 08/20/2013 01:27 PM, Twayne wrote:
>>>>> I'm attempting to check for US and Canadian zip codes
>>>>> (postal codes). The US is easy; mostly just be sure it's
>>>>> five numerics and except "00000" and "99999". […]
<snip>
>> Replace this line with
>>
>> preg_match('/^(?![09]{5})\\d{5}(?:-?\\d{4})?$/',
>> $possibleZip,
>>
>> (add the delimiter).
>
> Which is still not correct, because '/[09]{5}/' matches "90909",
> which is thus designated not valid. ISTM that alternation is
> required here:
>
> preg_match('/^(?!0{5}|9{5})\\d{5}(?:-?\\d{4})?$/',
> $possibleZip);
Perhaps you might also keep invalid postal codes in an array to look
up before utilizing the regular expression.
<snip>
--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-24 12:52 +0200 |
| Message-ID | <1622651.Cd0mxx89pG@PointedEars.de> |
| In reply to | #10610 |
Curtis Dyer wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> Norman Peelman wrote:
>>>>> On 08/20/2013 01:27 PM, Twayne wrote:
>>>>>> I'm attempting to check for US and Canadian zip codes
>>>>>> (postal codes). The US is easy; mostly just be sure it's
>>>>>> five numerics and except "00000" and "99999". [â¦]
>
> <snip>
>
>>> Replace this line with
>>>
>>> preg_match('/^(?![09]{5})\\d{5}(?:-?\\d{4})?$/',
>>> $possibleZip,
>>>
>>> (add the delimiter).
>>
>> Which is still not correct, because '/[09]{5}/' matches "90909",
>> which is thus designated not valid. ISTM that alternation is
>> required here:
>>
>> preg_match('/^(?!0{5}|9{5})\\d{5}(?:-?\\d{4})?$/',
>> $possibleZip);
>
> Perhaps you might also keep invalid postal codes in an array to look
> up before utilizing the regular expression.
I did that exactly where you snipped.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
[toc] | [prev] | [next] | [standalone]
| From | Doug Miller <doug_at_milmac_dot_com@example.com> |
|---|---|
| Date | 2013-08-25 14:31 +0000 |
| Message-ID | <XnsA2276B1042D1Bdougmilmaccom@78.46.70.116> |
| In reply to | #10610 |
Curtis Dyer <dyer85@gmail.com> wrote in news:kva1hr$rc0$1@dont-email.me: > Perhaps you might also keep invalid postal codes in an array to look > up before utilizing the regular expression. What happens when new postal codes are added? Perhaps one might simply send a request to the USPS address validation API to find out if it's a valid zip code or not. Use the "City-State Lookup" tool described here: https://www.usps.com/business/web-tools-apis/address-information.htm I don't know if Canada Post has similar tools.
[toc] | [prev] | [next] | [standalone]
| From | Twayne <nobody@spamcop.net> |
|---|---|
| Date | 2013-08-25 13:21 -0400 |
| Message-ID | <kvdef5$851$1@speranza.aioe.org> |
| In reply to | #10613 |
On 2013-08-25 10:31 AM, Doug Miller wrote: > Curtis Dyer <dyer85@gmail.com> wrote in news:kva1hr$rc0$1@dont-email.me: > >> Perhaps you might also keep invalid postal codes in an array to look >> up before utilizing the regular expression. > > What happens when new postal codes are added? > > Perhaps one might simply send a request to the USPS address validation API to find out if it's > a valid zip code or not. Use the "City-State Lookup" tool described here: > > https://www.usps.com/business/web-tools-apis/address-information.htm > > I don't know if Canada Post has similar tools. > Someone said there was a CDN API but I haven't gotten to it yet, though I have seen it. Haven't tried to programmatically access it yet though; want to finish up what I've been doing. Thanks
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.php
csiph-web