Filter digits, accept input from numpad

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolae Fieraru

    #1

    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="JavaS cript" 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="retu rn
    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


  • Dr John Stockton

    #2
    Re: Filter digits, accept input from numpad

    JRS: In article <41c5745d@duste r.adelaide.on.n et>, dated Sun, 19 Dec
    2004 23:30:16, seen in news:comp.lang. javascript, Nicolae Fieraru
    <nospam@please. com> posted :[color=blue]
    >
    >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.[/color]

    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.demo n.co.uk/js-valid.htm>.

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

    Comment

    • Nicolae Fieraru

      #3
      Re: Filter digits, accept input from numpad

      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" <spam@merlyn.de mon.co.uk> wrote in message
      news:r29WBtFZAe xBFwRu@merlyn.d emon.co.uk...[color=blue]
      > JRS: In article <41c5745d@duste r.adelaide.on.n et>, dated Sun, 19 Dec
      > 2004 23:30:16, seen in news:comp.lang. javascript, Nicolae Fieraru
      > <nospam@please. com> posted :[color=green]
      >>
      >>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.[/color]
      >
      > 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.demo n.co.uk/js-valid.htm>.
      >
      > --
      > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4
      > ©
      > <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of
      > news:comp.lang. javascript
      > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates,
      > sources.
      > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,
      > links.[/color]


      Comment

      • Dr John Stockton

        #4
        Re: Filter digits, accept input from numpad

        JRS: In article <41c657cc$1@dus ter.adelaide.on .net>, dated Mon, 20 Dec
        2004 15:40:39, seen in news:comp.lang. javascript, Nicolae Fieraru
        <nospam@please. com> posted :[color=blue]
        > 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.[/color]

        No, I see entry from the numeric pad working. Nevertheless, I dislike
        your whole approach.
        [color=blue]
        >Any other ideas?[/color]

        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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Lee

          #5
          Re: Filter digits, accept input from numpad

          Nicolae Fieraru said:[color=blue]
          >
          >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?[/color]

          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.

          Comment

          Working...