Solved

How to format zip code using javascript

Posted on 2007-03-29
6
666 Views
Last Modified: 2009-06-15
Hi Experts,

How to format zip code .The situation is if we enter a 9 digit code then it will be formatted to
***** - ****(5digits-4digits).also another condition is if we enter 7 digit code then it is formatted to
**** - ***(4digits - 3 digits). while leaving

Thanks in Advance
0
Comment
Question by:Rajesh_mj
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18821921
Like this:

<script>
function setZip(theField){
  var num = theField.value.replace(/\D/g,"");
  if(num.length==7){
    theField.value = num.substr(0,4)+"-"+num.substr(4);
  }
  if(num.length==9){
    theField.value = num.substr(0,5)+"-"+num.substr(5);
  }

}
</script>
<body>
<form>
<input type="text" name="zip" onBlur="setZip(this)">
</form>
</body>

0
 
LVL 6

Author Comment

by:Rajesh_mj
ID: 18822271
Hi,
     Thanks for the reply first............

The above is ok but there is one problem exist because we restrict the MaxLength of the Text box by "10".So the user can enter total 10 characters

For eg: The user can able to enter like this 55555-5555    Total length =10
In this case no need for formatting.The other case is
2)There is other possibility is to enter 5555555555  Total length = 10, in this case how we format this one.This is th eproblem mainly i face..

Thanks
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18822609
For that you need the built in method doWhatImean(;-)
Take spacial care for the passed parameters ;-)

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 18822632


<html>
<head>
<title>Zvonko &#42;</title>
<script>
function setZip(theField){
  var num = theField.value.replace(/\D/g,"");
  if(num.length==7){
    theField.value = num.substr(0,4)+"-"+num.substr(4);
  }
  if(num.length==9){
    theField.value = num.substr(0,5)+"-"+num.substr(5);
  }

}
function setLimit(theField, maxDigit){
  var num = theField.value.replace(/\D/g,"");
  if(num>maxDigit){
    theField.value = theField.value.substr(0,maxDigit);
  }
}
</script>
<body>
<form>
<input type="text" name="zip" onBlur="setZip(this)" onKeyUp="setLimit(this,9)">
</form>
</body>
</html>

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question