zip code validation

nmarano
nmarano used Ask the Experts™
on
Experts-

I have this validation on a ColdFusion form.  If the user doesn't enter anything, the pop-up works.  If the user enters one digit, the form gets processed.  Is there a way for me to check for a minimum of 5 digits?

Any help would be appreciated
Thanks
Nick

function _CF_checkzip(object_value, required)
{
      //trim whitespace before we validate
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 5 )
        {
            return false;
        }
    }
      else{
        if( object_value.length == 5 )
        {
            return true;
        }
    }

      return _CF_checkregex(object_value, /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/, required);
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
http://geekswithblogs.net/MainaD/archive/2007/12/03/117321.aspx

^\d{5}(-\d{4})?$

Matches all US format zip code formats (e.g., "94105-0011" or "94105")
why are you doing the length checking before doing the regex checking? wouldn't this be good enough?

function _CF_checkzip(object_value, required)
{
      return _CF_checkregex(object_value, /^\s*([0-9]{5})(?:[-\s]*([0-9]{4}))?\s*$/, required);
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial