Filter digits, accept input from numpad

Discussion in 'Javascript' started by Nicolae Fieraru, Dec 19, 2004.

  1. 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
    Nicolae Fieraru, Dec 19, 2004
    #1
    1. Advertising

  2. JRS: In article <>, dated Sun, 19 Dec
    2004 23:30:16, seen in news:comp.lang.javascript, Nicolae Fieraru
    <> 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.
    Dr John Stockton, Dec 19, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > JRS: In article <>, dated Sun, 19 Dec
    > 2004 23:30:16, seen in news:comp.lang.javascript, Nicolae Fieraru
    > <> 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.
    Nicolae Fieraru, Dec 20, 2004
    #3
  4. JRS: In article <41c657cc$>, dated Mon, 20 Dec
    2004 15:40:39, seen in news:comp.lang.javascript, Nicolae Fieraru
    <> 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.
    Dr John Stockton, Dec 20, 2004
    #4
  5. Nicolae Fieraru

    Lee Guest

    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.
    Lee, Dec 20, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Trahan

    Alt-numpad characters in JTextField

    Richard Trahan, Jul 26, 2003, in forum: Java
    Replies:
    7
    Views:
    6,100
    Roedy Green
    Aug 3, 2003
  2. John D.
    Replies:
    0
    Views:
    415
    John D.
    Aug 19, 2003
  3. Allen
    Replies:
    6
    Views:
    8,338
    Mean Mike
    Sep 18, 2013
  4. zax75
    Replies:
    1
    Views:
    1,095
  5. Jah_Alarm

    numpad in idle

    Jah_Alarm, Oct 24, 2010, in forum: Python
    Replies:
    3
    Views:
    231
Loading...

Share This Page