Solved

Adding a '-' for a zipcode

Posted on 2012-03-16
5
207 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

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

705 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

15 Experts available now in Live!

Get 1:1 Help Now