Marcell Scarlett
Here is a small function I wrote to only allow numeric values to be entered into text boxes used for phone number entry.
function numbercheck(n){ var code = n.charCode? n.which: n.keyCode; if(code !=8 && code !=9){//allow backspace & tab if(code<48 || code > 57){ return false; } } }
In the second line, n.charCode isn't valid in Internet Explorer, so this provides a quick browser check. If n.charCode returns a value this must not be IE, therefore n.which is also valid.Other than that, IE uses n.keyCode. Since we want to allow tab and backspace, we check for 8 or 9 which are the Javascript key codes tied to those buttons. We also want to allow for key codes between 48 and 57 which are all of the number keys.
We tie this function to the input boxes onKeyPress attribute like the example below and thats it.
Tags:
Related posts
Comments
7/3/2009 5:49:56 PM #
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
sulumits retsambew
9/27/2009 3:26:59 AM #
I have recently set off to develop using Javascript in the client browser but I am finding it is a large learning curve. The sentence " Javascript Phone Number Entry " in this post, alright that made me curious. My recent experience is with php, mysql, most linux based tools and a little flash. The challenges of employing Javascript to produce a good client script that runs speedily in all the major browsers, Internet Explorer, Firefox, Safari and Chrome can be a great headache that is taking me umpteen hours to overcome. I feel the tutorial websites are frequently strict and mention the identical points, comments and dialog in blogs frequently reference actual ways to resolve issues which takes me through the learning curve more quickly. Engrossing to read your opinions and the remarks in your website on Javascript. Thanks for the article, it has helped in a small way to take me through the learning curve.
Pen
10/22/2009 11:20:20 PM #
Hmmm interesting stuff
cash loans
11/14/2009 6:12:59 AM #
Great post.
waggi
11/14/2009 11:15:33 AM #
I understand the other codes but what is the purpose of charCode is just like declaration of the variables?
Miami web design
11/28/2009 12:13:19 PM #
Just wanted to say thanks for this.
fast payday loans
12/2/2009 8:16:56 AM #
Thank you for your help!
pay day loans
12/26/2009 5:53:43 AM #
Just try to smile for about 2-3 mins then you can get back to work
Massachusetts Payday Loans
1/14/2010 5:47:49 AM #
You can never plan the future by the past.
2/21/2010 10:47:57 AM #
Hello! Do you have some news you want to contribute to and that you think our readers might also find interesting? We are planning to open our blog in a few weeks and I have already bookmarked some of your posts. Best wishes, Peter
Antwan Rosich
2/21/2010 5:34:38 PM #
Great post I bookmared it on Delicious and submitted on Digg. Hopefully it sends more people your way
stock market
2/22/2010 3:03:04 AM #
Great post I bookmared it on Delicious and submitted on Digg. Hopefully it sends more visitors your way
Daniel Millions
2/25/2010 3:28:37 AM #
Should I get a Dedicated Hosting? Currently I am using ixwebhosting but they keep turning off my websites because of high server load. Im getting about 3,000 unique views a day. What hosting should I get?
Fatcow Reviews
2/28/2010 3:09:33 AM #
Just wanted to say that you have some awesome content on your website. If it's OK I would like to use some of the information you provided on my webiste. If I link back to your website do I have your permission to do so?
2/28/2010 5:48:59 PM #
If you are open to having a guest blog poster please reply and let me know. I can provide you with unique content for your blog, thanks.
stock quotes
3/1/2010 6:15:49 AM #
WoW Mobiles is awesome! I get free mobile service with t-mobile because I refered 3 people to wow. You can too!
wow mobile
3/1/2010 12:42:38 PM #
Finally someone that actually knows what they are talking about - thank you!
unsecured loan
3/2/2010 2:01:56 AM #
I love watching movies online, it is way cheaper than going to the theaters.
watch movies online free
3/3/2010 3:33:32 AM #
Just wanted to give you a shout from the valley of the sun, great information. Much appreciated.
Towaco NJ Plumbing
3/3/2010 3:50:00 AM #
I enjoy watching movies online, it is way easier than going to the theaters.
hd movies
Powered by BlogEngine.NET 1.5.0.7 Theme by Marcell Scarlett
My name is Marcell. I am 21 years old and I work full time as a Web Developer. I love computers, cars and the Web.
None