Solved

How to format zip code using javascript

Posted on 2007-03-29
6
610 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now