SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    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

  2. #2
    SitePoint Wizard
    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;
            }
        } 

  3. #3
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Enthusiast
    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!

  5. #5
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Addict
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •