www.webdeveloper.com
Results 1 to 5 of 5

Thread: Validation for US and Canada zipcode help

  1. #1
    Join Date
    Mar 2005
    Posts
    53

    Unhappy Validation for US and Canada zipcode help

    I have tried different types of validation for both zipcodes and cannot get them to work. Originally I had the field in spry but apparently you can't validate both formats at the same time or I can't figure it out. So then I tried javascript and I can't get that to work. I really don't know anything about javascript so can't tell you what I am doing wrong. Here is the code I am using: If there is a better method or in my case a function that works I sure would appreciate some help

    Code:
    <script type="text/javascript">
        function validZip(zip)
    {
    if (zip.match(/^[0-9]{5}$/)) {
    return true;
    }
    zip=zip.toUpperCase();
    if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
    return true;
    }
    if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
    return true;
    }
    alert('*** Please enter a valid zip code.');
    return false;
    }
        </script>
    
    
    <input name="ZIP" type="text" id="ZIP" size="6" maxlength="10" onChange="validZip('zip')"/>

  2. #2
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    Hi,

    Not sure what is not working, does it return True when it shouldn't match?

    If thats the case it will just be the RegEx your using. Try the RegEx suggested here:
    http://geekswithblogs.net/MainaD/arc...03/117321.aspx

    regards


    Ribs

  3. #3
    Join Date
    Mar 2005
    Posts
    53

    Red face Validation of US and Canada Zipcodes

    I am sorry I should have explained myself better. No matter what zip I enter whether it be correct or not it pops up the box - Bad Zip. If I knew more about the RegEx code on where to put it or how it works into the script I would give it a try. If you have some insight on that it would be great.

  4. #4
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    can you post the zip you tried and the zips you want to pass?

    you would be just swapping the RegEx part of your code you have now.

    Code:
    <script type="text/javascript">
        function validZip(zip)
    {
    if (zip.match(/^[0-9]{5}$/)) {
    return true;
    }
    zip=zip.toUpperCase();
    if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
    return true;
    }
    if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
    return true;
    }
    alert('*** Please enter a valid zip code.');
    return false;
    }
        </script>
    
    
    <input name="ZIP" type="text" id="ZIP" size="6" maxlength="10" onChange="validZip('zip')"/>
    The bits in bold are the RegEx you want to swap with one that works for what your trying to enter and match. I'm not that sure of US and Canada zips being from Scotland so you need to post samples of what your trying to match.

    regards


    Ribs

  5. #5
    Join Date
    Mar 2005
    Posts
    53

    Validation of US and Canada Zipcodes

    A couple of examples would be 63670, 63775, 63627 (5 digit numeric) for US. For Canada A0A 1J0 (alphanumeric).

    So in order to use the example RegEx you suggested this is what the code would look like?

    Code:
    <script type="text/javascript">
        function validZip(zip)
    {
    if (zip.match(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)) {
    return true;
    }
    alert('*** Please enter a valid zip code.');
    return false;
    }
        </script>
    
    
    <input name="ZIP" type="text" id="ZIP" size="6" maxlength="10" onChange="validZip('zip')"/>

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