473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Filter digits, accept input from numpad

Hi All,

I have some javascript code and I try to filter out any characters (which
are not digits or delete) from an input element in a form.

Here is the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> JavaScript </TITLE>
<script language="JavaScript" type="text/JavaScript">
<!--
function filter(evt) {
var keyCode = evt.which ? evt.which : evt.keyCode;
return (keyCode >= '0'.charCodeAt() && keyCode <= '9'.charCodeAt()) ||
keyCode == 8;
}
-->
</script>
</HEAD>

<BODY>
<form method="post" action="" name="form">
<input maxLength=5 size=4 name='25' value="" ONKEYDOWN="return
filter(event)">
</form>

</BODY>
</HTML>

The problem with this code is that it doesn't accept input from the numpad.
Is there a way to make it work with the numpad as well?
Regards,
Nicolae
Jul 23 '05 #1
4 9528
JRS: In article <41******@duster.adelaide.on.net>, dated Sun, 19 Dec
2004 23:30:16, seen in news:comp.lang.javascript, Nicolae Fieraru
<no****@please.com> posted :

I have some javascript code and I try to filter out any characters (which
are not digits or delete) from an input element in a form.


Delete may be a keystroke, but it is not a character.

ISTM far simpler, and better, to indicate clearly enough what sort of
characters are needed, and to use a RegExp test onClick to check either
that it contains no non-digits or that it contains only [a reasonable
number of] digits. If the check fails, alert and refocus; otherwise, it
is safe to convert to Number with a unary + operator.

See FAQ and <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #2
Hi,

Thank you for your reply. I don't want to popup alerts if somebody enters
the wrong information. At present, my script simply doesn't type in any
characters (or keystrokes) other than numbers and back space (which is what
I want). Everything works perfectly, the only problem I have is that it
doesn't accept entry from the numpad. If you try the simple web page I
provided, you can see what I am looking for. Any other ideas?

Regards,
Nicolae

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:r2**************@merlyn.demon.co.uk...
JRS: In article <41******@duster.adelaide.on.net>, dated Sun, 19 Dec
2004 23:30:16, seen in news:comp.lang.javascript, Nicolae Fieraru
<no****@please.com> posted :

I have some javascript code and I try to filter out any characters (which
are not digits or delete) from an input element in a form.


Delete may be a keystroke, but it is not a character.

ISTM far simpler, and better, to indicate clearly enough what sort of
characters are needed, and to use a RegExp test onClick to check either
that it contains no non-digits or that it contains only [a reasonable
number of] digits. If the check fails, alert and refocus; otherwise, it
is safe to convert to Number with a unary + operator.

See FAQ and <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of
news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,
links.

Jul 23 '05 #3
JRS: In article <41********@duster.adelaide.on.net>, dated Mon, 20 Dec
2004 15:40:39, seen in news:comp.lang.javascript, Nicolae Fieraru
<no****@please.com> posted :
Everything works perfectly, the only problem I have is that it
doesn't accept entry from the numpad. If you try the simple web page I
provided, you can see what I am looking for.
No, I see entry from the numeric pad working. Nevertheless, I dislike
your whole approach.
Any other ideas?


As I wrote before, read (and heed) the newsgroup FAQ, especially 2.3,
particularly but not exclusively paragraph 6; and read <URL:http://www.
merlyn.demon.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Lee
Nicolae Fieraru said:

Hi,

Thank you for your reply. I don't want to popup alerts if somebody enters
the wrong information. At present, my script simply doesn't type in any
characters (or keystrokes) other than numbers and back space (which is what
I want). Everything works perfectly, the only problem I have is that it
doesn't accept entry from the numpad. If you try the simple web page I
provided, you can see what I am looking for. Any other ideas?


It's [almost?] always a bad idea to restrict input in that way.

The user who tries to type something else is more likely to decide that your
page is broken than that he is doing something wrong, and your method doesn't
provide any feedback to correct him.

The user who isn't a perfect typist may accidentally hit the letter "r" while
reaching for the "4". Realizing his mistake, he hits the backspace, types the
"4" and proceeds with his input, never noticing that since you disallowed the
"r", he actually deleted the previous correct digit.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Karen | last post by:
Hi there, in my asp form i would like to have a field which can ONLY accept digits BUT can accept special characters as well such as: &,$$,##. i.e: if the user enters : 147hhh, or P44556HH or...
0
by: Jack Li | last post by:
Hi, Why in the DataGrid control, when NumPad minus key is pressed, there is no response, all other keys are OK. Thanks Jack
18
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the...
0
by: net2knot | last post by:
i want to send numpad enter to a dos application. i used sendkeys but none of the use. i tried a module but it sends the standard enter key rather than numpad enter key, since standard enter key...
1
by: net2knot | last post by:
i want to send numpad enter to a dos application. i used sendkeys but none of the use. i tried a module but it sends the standard enter key rather than numpad enter key, since standard enter key...
0
by: net2knot | last post by:
want to send numpad enter to a dos application. i used sendkeys but none of the use. i tried a module but it sends the standard enter key rather than numpad enter key, since standard enter key and...
1
by: net2knot | last post by:
want to send numpad enter to a dos application. i used sendkeys but none of the use. i tried a module but it sends the standard enter key rather than numpad enter key, since standard enter key and...
1
by: net2knot | last post by:
want to send numpad enter to a dos application. i used sendkeys but none of the use. i tried a module but it sends the standard enter key rather than numpad enter key, since standard enter key and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.