Filter digits, accept input from numpad

N

Nicolae Fieraru

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 19 Dec
2004 23:30:16, seen in Nicolae Fieraru
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>.
 
N

Nicolae Fieraru

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 said:
JRS: In article <[email protected]>, dated Sun, 19 Dec
2004 23:30:16, seen in Nicolae Fieraru
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. [email protected] Turnpike v4.00 IE 4
©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of
<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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 20 Dec
2004 15:40:39, seen in Nicolae Fieraru
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>.
 
L

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top