SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate form field for numbers only

    I have the following code:

    Code:
     // check for zip
     if (document.form1.zip.value == "") {
     invaliddata += "\n - Zip is missing.";
     }
    This tests that something exists. What I'd like to do is alter this to test that 5 numbers(zipcode) and only 5 numbers are entered. Can anyone help?

  2. #2
    SitePoint Member
    Join Date
    Jan 2003
    Location
    Bakersfield, CA
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Affordable Web Hosting & Design
    http://www.c-double.com

  3. #3
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks... but that's more than I need.

    I've managed to get it to something like this:

    Code:
     
     function checkForm() {
     var invaliddata = "";
     digits="0123456789"
     
     for(i=0; i<5; i++) {
        var charac = document.form1.zip.charAt(i);
        if (digits.indexOf(charac<0)) {
        invaliddata += "\n - Zip is missing.";
        }
        }
     
     }
     </script>
    But this gives me the following error:

    Error: document.form1.zip.charAt is not a function

    All I'm trying to do is validate a zip code... surely someone has done this before?!?!?

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Regular expressions to the rescue!
    Code:
    function checkForm() 
    {
     var invaliddata = "";
     var zipCheck = /^\d{5}(-\d{4})?$/;
     
     if (!zipCheck.test(document.form1.zip.value))
     {
        invaliddata += "\n - Zip is missing.";
     }
     
    }
    That should validate a zip code in either the 5-digit format or the 5+4 format.

  5. #5
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once again v-man... you are the man! Thanks!

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    You may also want to change the error message too. My code will check for both a missing zip code and an improperly formatted one.

  7. #7
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vinnie -

    You wouldn't happen to have an expression to check for a phone number would ya? Also - have any handy useful links so I can learn more about these?

  8. #8
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Phone number regular expression:
    Code:
    ^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?
    [-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$
    (this also checks extensions, for example (407)555-5555x1234).
    And a great regular expression resource: http://regexlib.com/

  9. #9
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - now THAT'S a handy website! Thanks again.


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
  •