Solved

How to format zip code using javascript

Posted on 2007-03-29
6
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clear input text 15 27
How to single quote a check box id using java script before using document.GetElementById ? 9 33
window close link 7 36
modify change color of text 9 39
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

730 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