Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to format zip code using javascript

Posted on 2007-03-29
6
Medium Priority
?
944 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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