.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

ZIP code validation using javascript

Posted By: Ramesh     Posted Date: February 15, 2009    Points:2   Category :ASP.Net
Hello friends,
I need to do ZIP code validation using javascript. Means i want to validate in the client side using javascript.

I am using ASP.Net.

Regards
Ramesh

Responses
Author: Amit Mehra             
Posted Date: February 16, 2009     Points: 5   

Hi
you can use below function to check the valid zip code using javascript.


function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
return false;
}
}
return true;
}


http://javascript.internet.com/forms/val-zip-code.html

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Nabin             
Posted Date: August 27, 2009     Points: 5   

=============================================
insert that script in<head></head>
=============================================

<SCRIPT LANGUAGE="JavaScript">
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
return false;
}
}
return true;
}
// End -->
</script>


=========================================================================
insert that part inside <body> where u want to use that script function
=========================================================================

<center>
<form name=zip onSubmit="return validateZIP(this.zip.value)">
Zip: <input type=text size=30 name=zip>
<input type=submit value="Submit">
</form>
</center>



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend