FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » CTYPE
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: CTYPE THREAD RESOLVED [message #182680 is a reply to message #182670] Mon, 26 August 2013 18:07 Go to previous messageGo to previous message
Fiver is currently offline  Fiver
Messages: 35
Registered: July 2013
Karma:
Member
On 2013-08-25 21:35, Twayne wrote:
> If anyone is interested, here's the code I ended up with. It still
> needs to validate alphabetic & numeric array positions but that should
> be reasonably simple to add by checking the array positions for alpha or
> digit characters.
>
> Thanks for all the help!
> -----------------------------------
> $zip = check_input($zip);
> $zip = strtoupper($zip);
> // DETERMINE IF US POSTAL CODE
> if(strlen($zip)===5) {
> echo "U.S. postal code used "; }
> IF (is_numeric($zip)) {
> echo " and is 5 digits.";
> }
> IF(strlen($zip) === 7) {
> echo "Zip appears to be Canadian . <br />";
> }
> // ================== NOW LOOK TO SEE IF IT'S ALPHA-NUMERIC
>
> // ================ LET'S ASSIGN IT TO AN ARRAY
> echo ' <pre>';
> print_r(str_split($zip,1));
> echo '</pre>';
>
> // ================= check space is where it belongs
> if($zip[3] === " ") {
> echo "<br />Space in correct place<br />";
> }
>
> $sUser = $zip;
> $aValid = array(' ');
> if(!ctype_alnum(str_replace($aValid, '', $sUser))) {
> echo "Username has a problem";
> echo 'Your Postal Code is NOT properly formatted.';
> }
> else {
> echo "Postal code is properly formatted as ALPHA NUMERIC with ONE SPACE
> as the 4th character";
> }

For the US zip codes:

is_numeric() is not the correct function to use here. "+.1e1" and
"0xCAB" are both numeric strings and would pass as US zip codes.
Better solution:

if (strlen($zip) == 5 && ctype_digit($zip)) {
echo "looks like a US zip code\n";
}

Better yet: check for known bad zip codes as well

... && $zip != "00000" && $zip != "99999" && I don't know, some more

For the Canadian zip codes:

Your code has a comment "let's assign it to an array", and you also
wrote that you still needed to validate the array positions, but you
never do create an array (except for the unnecessary one in $aValid,
which should probably be a string).

Anyway, you're right that you still need to check the characters to the
right and left of the space, or strings like "x " and " 3 "
would be seen as valid zip codes. Here's one way to do it:

if (strlen($zip) == 7
&& $zip[3] == " "
&& ctype_alpha($zip[0] . $zip[2] . $zip[5])
&& ctype_digit($zip[1] . $zip[4] . $zip[6]))
{
echo "looks like a Canadian zip code\n";
}

Note the two closing parens after $zip[6] ;)


regards,
5er


PS: No regexes were killed or injured in the production of this message.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: korean character sets
Next Topic: Android app Developers requirements
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 07 00:13:46 GMT 2024

Total time taken to generate the page: 0.06653 seconds