Solved

Adding a '-' for a zipcode

Posted on 2012-03-16
5
209 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
  • 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 400 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 100 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

770 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