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

x
?
Solved

Adding a '-' for a zipcode

Posted on 2012-03-16
5
Medium Priority
?
214 Views
Last Modified: 2012-03-20
When a user is inputting a zipcode into a textbox I want the '-' automatically inputted when they type in their 6th number.  How do I do this?
0
Comment
Question by:huerita37
[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
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37731552
check this cool plugin : http://digitalbush.com/projects/masked-input-plugin/

$("#zip").mask("999999-99");

Open in new window

0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1600 total points
ID: 37732193
See if this jsfiddle helps

The main bit of code is:
$("#zip").keyup(function(event) {
    var value = $(this).val();
    var pattern = /^(\d{6}-\d{0,2}|\d{0,6})$/;
    if (!pattern.test(value)) {
        $(this).val($(this).val().replace(/\D/g, "").replace(/(\d{6})/, "$1-").replace(/(\d{6}-\d{1,2}).*/, "$1"))
    }
});

Open in new window

0
 

Author Comment

by:huerita37
ID: 37740120
These are both good options but I don't know how to call them.

leakim971: Where do I put your code?  I read the link you attached but I don't really understand jquery.


Proculopsis: I understand yours better because the link shows me how to call the function, but where do I add the function?  When I tried to add it between:


<script language="JavaScript" type="text/javascript" src="scripts/jquery.js"></script>
<script>

   'Added the code here

</script>

I got an error when saving.  It said 'Some Unicode character in this file could not be saved in the current codepage...'

I must be adding the code in the wrong spot.

Any suggestions?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 400 total points
ID: 37740141
You need to put the following lineS in the head section of your page :
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>

Open in new window

DON'T FORGET TO UPDATE THE PATH OF BOTH FILES YOU DOWNLOAD AND PUT IN YOUR WEB SITE FOLDER :
jquery.maskedinput.js
jquery.js

Now, use the following :
<script type="text/javascript">
$(document).ready(function() {
    $("#zip").mask("999999-99"); // HERE << zip >> is the value of the ID attribute of your zip field
})
</script>

Open in new window

0
 

Author Closing Comment

by:huerita37
ID: 37743623
I gave leakim971 100 points because he showed me how to use jquery.  He didn't get all the points because I couldn't get the code to run in my program.

 Proculopsis got 400 points but I did need to change the numbers.  I needed 5 digits then '-' and then 4 digits.

Thanks guys for such good help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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