By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

Filter digits, accept input from numpad

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.