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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question simple regular expressions question

    I'm having a problem making the regular expression for U.S. zip code verification work.
    the regular expression: /(^\d{5}$)|(^\d{5}-\d{4}$)/

    My code is below. No matter what I type in, it asks me to correct my zip code. Any suggestions?

    Code:
    <html>
    <head>
    	<title>U.S. Address</title>
    <script language="Javascript" type="text/javascript">
    <!-- hide script from old browsers
    function validateForm()
    {
    if (document.forms[0].elements[0].value == "")
    	{
    	alert ("please enter an address!");
    	return false;
    	}
    if (document.forms[0].elements[1].value == "")
    	{
    	alert ("please enter a city!");
    	return false;
    	}
    if (document.forms[0].elements[2].value == "--")
    	{
    	alert ("please select a state!");
    	return false;
    	}
    if (document.forms[0].elements[3].value == "")
    	{
    	alert ("please enter a zip code!");
    	return false;
    	}
    if (document.forms[0].elements[3].value = "/(^\d{5}$)|(^\d{5}-\d{4}$)/")
    	{
    	alert ("please correct your zip code!");
    	return false;
    	}
    
    else {
    window.close();
    updateParent5(this)
    }
    }
    
    function updateParent1(textField){
    opener.document.contactinfo.address.value=textField.value
    }
    function updateParent2(textField){
    opener.document.contactinfo.city.value=textField.value
    }
    function updateParent3(textField){
    opener.document.contactinfo.state.value=textField.value
    }
    function updateParent4(textField){
    opener.document.contactinfo.zip.value=textField.value
    }
    function updateParent5(textField){
    opener.document.contactinfo.country.value='USA'
    }
    // end hiding -->
    </script>
    </head>
    
    <body>
    <form action="us-add.php" onsubmit="return validateForm()">
    address: <input type="text" onblur="updateParent1(this)"><br>
    city: <input type="text" onblur="updateParent2(this)"><br>
    state: <select onblur="updateParent3(this)">
    	<option value="--">select</option>
    <!-- lots of U.S. states here -->
    </select>
     zip: <input type="text" name="zip1" onblur="updateParent4(this)"><br>
    country: USA<br><br>
    <input type="submit" name="submit">
    </form>
    
    </body>
    </html>

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this one:

    Code:
    <script type="text/javascript">
      function chkZip(input){
        if(!input.match(/^[0-9]{5}([- /]?[0-9]{4})?$/)){
            alert ("please correct your zip code!");
            return false;
        }
      }
    </script>
    </head>
    
    <body>
    <form>
    <input type="text" onblur="chkZip(this.value)">
    </form>
    .....Willy

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply. This looks like some good code but I already have an onblur event on that field.

    I tried this:
    Code:
    if(!/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(document.forms[0].elements[3].value))
    	{
    	alert ("please correct your zip code!");
    	return false;
    	}
    And it worked.

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Watch using \d as it will allow decimal points/periods...
    BTW: I was not implying that you needed to use onblur (although I would), it was only provided as a working example....

    ......Willy

  • #5
    New Coder
    Join Date
    Jun 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, thanks. I didn't know about the decimals.


  •  

    Posting Permissions

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