SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
May 13, 2009, 10:02 #1
- Join Date
- Aug 2006
- Posts
- 86
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
add word NONE to zip in validation
hi @LL,
we are using simple code for validation in zip, but now we want to be able to type the word
"NONE" also.
How this code can be modified to do that?
case 'zip':
if ($inString=='') { appendError("Required Field"); }
else {
$length=strlen($inString);
switch($length)
{
default:
appendError("Must be 5 digit zip '00000', or zip+4 '00000-0000' or 'NONE'");
break;
case '5':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Only numbers allowed in zip"); }
break;
case '7':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:space:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Canadian postal codes must be three digits, one space, three digits"); }
break;
case '10':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Not a valid Zip+4 format"); }
break;
Thanks
-
May 13, 2009, 12:53 #2
- Join Date
- Mar 2002
- Location
- Bristol, UK
- Posts
- 2,240
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Something like this should work:
PHP Code:case 'zip':
if ($inString=='') {
appendError("Required Field");
} elseif($inString != 'NONE') {
$length=strlen($inString);
switch($length)
{
default:
appendError("Must be 5 digit zip '00000', or zip+4 '00000-0000' or 'NONE'");
break;
case '5':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Only numbers allowed in zip"); }
break;
case '7':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:space:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Canadian postal codes must be three digits, one space, three digits"); }
break;
case '10':
$pattern="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]][[:digit:]][[:digit:]]$";
$emailTest=ereg($pattern, $inString);
if (!$emailTest) { appendError("Not a valid Zip+4 format"); }
break;
}
}
-
May 13, 2009, 13:00 #3
SJH already replied it. Maybe you may also accept 'none', 'None' ...
Code PHP:(strtolower($inString)!='none')
Tweep List adds an avatar menu to Twitter (open source)
Word Stats shows your most used words on Twitter
-
May 13, 2009, 13:28 #4
- Join Date
- Aug 2006
- Posts
- 86
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
SJH,
that worked perfectly!!
Thanks!!
glenngould,
Can we modify it so it will take lower & upper case?
for ex. 'none' or 'NONE' ?
THANKS!
-
May 13, 2009, 15:38 #5
The one I wrote does that already (also for 'nOne' 'None' etc.).
Tweep List adds an avatar menu to Twitter (open source)
Word Stats shows your most used words on Twitter
-
May 13, 2009, 21:41 #6
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Canadian postal codes are not all digits, ie: T1B 4A5. You would need to change your pattern and error message for case 7.
PHP Code:$pattern="^[[:alpha:]][[:digit:]][[:alpha:]][[:space:]][[:digit:]][[:alpha:]][[:digit:]]$";
Bookmarks