Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem validating postal code 1

Status
Not open for further replies.

nomu26

Programmer
Sep 10, 2003
22
ZA
hi guys could you please help me I've done my script checking for input data but now I have a problem including checking for number validation in Postal Code field: here's my code and on the Postal code instead of checking for empty field I need to change it to validate number only:
function isformvalid()
{
//Load Vars from form
txtFirst_Name = document.frmValidate.txtFirst_Name.value;
//alert(txtFirst_Name);
txtSurname= document.frmValidate.txtSurname.value;
txtEmail= document.frmValidate.txtEmail.value;
txtPostal_Code= document.frmValidate.txtPostal_Code.value;

msgHeader = "The form contains the following errors:\n\n";
errMsg = "";
numErrors = 0;


var focusElement;
//First Name
if(txtFirst_Name==""){
errMsg += "Your first name is required,\n";
if(focusElement == null)
focusElement = document.frmValidate.txtFirst_Name;
document.getElementBy("firstNameError").className= "show";
numErrors = numErrors + 1;
} else {
document.getElementBy("firstNameError").className= "hide";
}

//Surname
if(txtSurname==""){
//alert("here");
errMsg += "Your Surname is required,\n";
if(focusElement == null) focusElement=document.frmValidate.txtSurname;
document.getElementById("surnameError").className = "show";
numErrors = numErrors + 1;
} else {
document.getElementById("surnameError").className = "hide";
}

//Email
if(txtEmail==""){
//alert("here");
errMsg += "Your Email is required,\n";
if(focusElement == null) focusElement = document.frmValidate.txtEmail;
document.getElementById("EmailError").className = "show";
numErrors = numErrors + 1;
} else {
document.getElementById("EmailError").className = "hide";
}

//Postal Code
if(txtPostal_Code==""){
errMsg += "Enter Postal Code is required.\n";
if(focusElement == null) focusElement = document.frmValidate.txtPostal_Code;
document.getElementById("CodeError").className = "show";
numErrors = numErrors + 1;
} else {
document.getElementById("CodeError").className = "hide";
}


//Check to see if we had any errors
if(numErrors>0){
alert(msgHeader + errMsg);
if(focusElement)
focusElement.focus();
return false;
}else{
return true;
}
}

-->
</script>
 
What postal code format are you checking for?
xxxxx -or- xxxxx-xxxx

2b||!2b
 
This works for the &quot;Zip+4 code. If all you need is 5 digits, remove the -[\b] in the Chars variable.

Code:
if (document.formname.zip.value.length > 0) {
     var Chars = &quot;0123456789-&quot;;
	 for (var i = 0; i <= document.formname.zip.value.length; i++) {
	      if (Chars.indexOf(theForm.zip.value.charAt(i)) == -1){
		  	  alert(&quot;Your Zip Code Contains Invalid Characters!\n\nValid characters are: 0123456789-&quot;);
              document.formname.zip.focus();
              return (false);}}}[\code]

There's always a better way.  The fun is trying to find it!
 
If you're looking for Postal Code (CDN) validation, here's your JavaScript function. This function will validate against the following patterns (where &quot;a&quot; is alpha) a#a-#a#, a#a #a#, a#a#a#

function Validate(val)
{
var regEx = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/;
if(regEx.test(val))
{
alert('Valid Postal Code');
}
else
{
alert('Invalid Postal Code');
}
}

If you're looking for validation for Zip code (US), try the following:

function Validate(val)
{
var regEx = /[0-9]{5}/;
if(regEx.test(val))
{
alert('Valid Zip Code');
}
else
{
alert('Invalid Zip Code');
}
}

Depending on the length you need to check for, adjust the {5} accordingly.

Regular Expressions r0x0r!!! w00t!! =D

-----------------------------------------------
&quot;The night sky over the planet Krikkit is the least interesting sight in the entire universe.&quot;
-Hitch Hiker's Guide To The Galaxy
 
this is the regular expression for validating zip-code
\d{5}(-\d{4})?

It validates say ....
32678
32678-0909

but I get an error for
326780909

... how can I modify the regular expression to allow 9 digits without the dash if entered correctly say 326780909
?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top