www.webdeveloper.com
Results 1 to 2 of 2

Thread: JavaScript form validation

  1. #1
    Join Date
    Jun 2003
    Posts
    3

    JavaScript form validation

    I've got a form that I want to test to make sure the user entered a zip code in a valid array and I'm not sure how to check it...

    The zip code list is like 11111, 11114, 11117, so I can't just include a range...

    I had written a function like:

    Code:
    function validate_zip(field,alerttxt) { 
         with (field) { 
              if (value==null||(value<"11111"||value>"11174")) {
                  alert(alerttxt);
                  return false;}
              else {return true} 
          } 
    }
    but, I have to exclude the non-inclusive zip codes...so, how do I create an array and test against it???

    TIA!!!

  2. #2
    Join Date
    Jun 2003
    Posts
    3
    Got it!!

    Code:
    <script type="text/javascript">
    var zipArray = new Array("11111","11112","11113","11114");
    
    function inArray(v,a)
    {
        for(i = 0; i < a.length; i++)
        {
            if(v == a[i])
            {
                return true;
            }
        }
        return false;
    }
    
    // Usage
    alert(inArray('11114',zipArray));// returns true
    alert(inArray('11115',zipArray));// returns false
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles