Forums

This topic is locked

Validation for US and Canada zipcodes in the same field

Posted 07 Jul 2011 20:53:37
1
has voted
07 Jul 2011 20:53:37 Michelle Weiler posted:
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.


<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')"/>

Replies

Replied 15 Sep 2011 17:15:49
15 Sep 2011 17:15:49 Patrick Woldberg replied:
Simplified validation function:
function validZip(zip) {
  var reUS = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/, // US Zip
      reCA = /^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/; // CA Zip

  return zip.match(reUS) || zip.match(reCA);
}


To check the value when input is edited:
<input name="ZIP" type="text" onchange="if (validZip(this.value)) {alert('*** Please enter a valid zip code.');}"/>


The above gives an alert, you probably want to change this behavior and make for example the input red.

Finally you will need to capture the submit and check everything again and return false there to prevent the submit if something is wrong.

Also always implement server-side validation, spammers can easily get around javascript validation.
Replied 24 Oct 2011 16:45:51
24 Oct 2011 16:45:51 Michelle Weiler replied:
First thanks for your help. I am just getting back to this code. I put in your code and it validates the Canadian zip but not the US. I am not sure what is happening. Here is the code that I am using and it is supposed to validate based on the country selection. I replaced my code with your validation so not sure if the other portion of code has to be adjusted.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script> 
function checkForOther(obj) { 
 test = obj.value
 if (!document.layers) { 
 var txt = document.getElementById("otherTitle"); 
 if (obj.value == "US")
 txt.style.display = "inline";
 
 else (obj.value == "CA")
 txt.style.display = "inline";
 { 
  } 
 } 
} 
</script>
 
  <script type = "text/javascript">
  
   function validZip(zip) {  
      var reUS = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/, // US Zip  
         reCA = /^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/; // CA Zip  
     
    return zip.match(reUS) || zip.match(reCA);  
}  			  

</script>
</head>

<body>

<form action="https://www.amm.org/memserv/survey/surveypg2.asp" method="post" name="survey1" id="survey1" onKeyUp="highlight(event)" onClick="highlight(event)">

  <p><strong>Country:&nbsp;</strong>
    <select name="selTitle" id="titles" onchange="checkForOther(this)"> 
      <option value="0">**</option> 
      <option value="CA">Canada</option> 
      <option value="US">United States</option> 
    </select> 
  </p>
  <p><strong>US Zip Code:&nbsp;&nbsp;</strong>
  <input name="ZIP" type="text" id="otherTitle" onchange="if (validZip(this.value)) {alert('*** Please enter a valid zip code.');}"/>
    
  </p>
</form>
</body>
</html>


Replied 25 Oct 2011 16:36:58
25 Oct 2011 16:36:58 Michelle Weiler replied:
Also you mentioned always use server side validation that spammers can get around javascript. Is there a better way to do this besides javascript? If so I am open to any suggestions. I tried doing this in asp but didn't have any luck since the country drop down box doesn't contain a submit button to tell the zip field which country was selected. So if there is a way to do this in asp please let me know how you would do this. Thanks!
This reply was removed on 3/2/2012 9:26:32 AM.
See the changelog
This reply was removed on 3/16/2012 8:41:13 AM.
See the changelog

Reply to this topic