Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Validation for US and Canada zipcode help

    I have tried many different scripts Regex and different javascripts but nothing seems to work. The code I will send is about the closest I have gotton but it pops up a box whether it is valid or not. I just really know nothing about javascript to even know what to change. Please if someone has an answer or better method I would really appreciate it.

    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
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi

    Validation for US and Canada zipcode help
    US Zip code 5 or 5-4 digits--- I believe it was written by Philip M on this forum but I don't have the link to the page.


    Code:
    <html>
    <title>US Zip code 5 or 5-4 digits</title>
    <head>
    <script type = "text/javascript">
    function addHyphen() {
    var z = document.getElementById("zipcode").value;
    z = z.replace(/[^0-9-]/, "");
    if (/^\d{6}/.test(z)) {
    z = z.substring(0,5) + "-" + z.substring(5);
    }
    document.getElementById("zipcode").value = z;
    }
    
    function chkZip() {
    var z = document.getElementById("zipcode").value;
    if (!/^\d{5}(-\d{4})?$/.test(z)) {
    alert ("Invalid zip code - please re-enter!");
    document.getElementById("zipcode").value = "";
    return false;
    }
    }
    </script>
    </head>
    <body>
    
    Zip Code <input type = "text" id = "zipcode" size = "10" maxlength = "10" onkeyup = "addHyphen()" onblur = "chkZip()">
    
    </body>
    </html>
    LT

    Just realised this script doesn't deal with letters and the op needed that;(
    But I'll leave this code here because someone may search for Validation for US and Canada zipcode
    in the future and only need digits.
    Last edited by low tech; 07-09-2011 at 01:22 AM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Your call to the function is bogus!

    You are doing
    Code:
        validZip('zip')"
    which means that you are *ALWAYS* asking validZip to test to see if the *STRING* 'zip' matches one of the pattern.

    Since "zip" would only match the pattern /^[A-Z][A-Z][A-Z]$/ (after you do the toUpperCase() on it) of course it doesn't match any of your three patterns.

    *NOW* try doing:
    Code:
        onchange="validZip(this.value)"
    You still have a bug in one of your RegExp's. You *think* you are allowing a period in the middle of the Canadian postal code: A1A.1A1 In actuality, the period means "match ANY character" so you would match A1A?1A1 or even A1AB1A1 or ...

    You need to "escape" the period (force an actual period) by putting a \ in front of it.

    Finally, the whole thing could be done much simpler:
    Code:
    function validZip( zip )
    {
        var zipre = /^(\d{5}|[A-Z]\d[A-Z]\.?\d[A-Z]\d)$/i;
        if ( zipre.test( zip ) ) return true;
        alert("Not a valid zip or postal code");
        return false;
    }


  •  

    Posting Permissions

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