Solved

Adding a '-' for a zipcode

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

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

623 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