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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts

    zipcode edit field

    Hello all Helper,

    Im using the code below to validate the zipcode field, but not the way i want it to be. The problem is zipcode field is not a required a field client allow to be able to submit without entering data on zipcode field. In the other hands, if client fill out the zipcode field less than 5 numeric then the validate message pop up say something (invalid zipcode). In my HTML code i set onblur="ValidateUSZip()" but everytime i tabbed over the field and received the message popup. I wanted the field to validate when client edit the field, if they are not edit the parameter will pass through. Please help to solve this problem. I'd appreciate any help.

    Thanks,
    Peter.

    <script language="Javascript">
    function ValidateUSZip() {
    var re=/\d{5}/;
    if(!re.test(document.forms[0].PrimaryZip.value))
    alert("The zip code field must be five digits long. (ex: 40202)");
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    477
    Thanks
    0
    Thanked 51 Times in 50 Posts
    Check to see if the field is blank first. If it is blank you don't need to process it.

    <script language="Javascript">
    function ValidateUSZip() {
    if(document.forms[0].PrimaryZip.value != ""){
    var re=/\d{5}/;
    if(!re.test(document.forms[0].PrimaryZip.value))
    alert("The zip code field must be five digits long. (ex: 40202)");
    }
    }
    </script>

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help Kansel

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem i got when tesing the application. Every time i edited the field and it won't let me tab over to next field. Do you know why, please help me..that is a only problem i have right now.

    Thanks again,

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    477
    Thanks
    0
    Thanked 51 Times in 50 Posts
    With the script from my last post and using the following test form, this seems to work correcly.

    <form>
    <input>
    <input name="PrimaryZip" onblur="ValidateUSZip()">
    <input>
    </form>

    I can tab over the field without putting any info in and no alert box. If I put data in, it has to contain a 5-digit number in order to pass the test. If I put in less than 5 digits it alert()s.

    If you're having problems with this, could you post the complete form code or a link to the complete code for me to debug?

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a complete code and it won't let me tab over to next field

    <script language="Javascript">
    function ValidateUSZip() {
    if(document.forms[0].PrimaryZip.value != ""){
    var re=/\d{5}/;
    if(!re.test(document.forms[0].PrimaryZip.value))
    alert("The zip code field must be five digits long. (ex: 40202)");
    document.form.PrimaryZip.focus();
    return false;
    }
    }
    </script>


    the reason i added document.form.PrimaryZip.focus() that i wanted the cursor will be redirected to the field, but the problem i got it is won't let me tab over to next field even i edit with 5 digits numeric. Please help me to debug it.

    Thanks,
    Peter

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    477
    Thanks
    0
    Thanked 51 Times in 50 Posts
    You only want the field to refocus if the zip is invalid. The way it is set up now it will refocus whether it is valid or not.

    In order to refocus only on invalid zip, you need to enclose that code in {braces}.

    if(!re.test(document.forms[0].PrimaryZip.value))
    alert("The zip code field must be five digits long. (ex: 40202)");
    document.form.PrimaryZip.focus();
    return false;

    should be changed to

    if(!re.test(document.forms[0].PrimaryZip.value))
    {alert("The zip code field must be five digits long. (ex: 40202)");
    document.forms[0].PrimaryZip.focus();
    return false;}

    If the test (!re.test(document.forms[0].PrimaryZip.value) passes, the script will execute the following commands:
    1) alert("The zip code field must be five digits long. (ex: 40202)")
    2) document.forms[0].PrimaryZip.focus()
    3) return false

    If the test fails (i.e. PrimaryZip is valid) the script will simply exit.

    -kansel

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mr. Kansel

    It's work great...i totally forgot to add the braces for an alert..

    Thanks again for your great help
    Peter

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My Validator does ZIPs!


  •  

    Posting Permissions

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