Solved

Adding a '-' for a zipcode

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 36
Html Newline 7 21
How to control cache of some js files ? 7 42
Only allow one check box 17 27
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

830 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