trapping/preventing keypress in <input> box

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

    trapping/preventing keypress in <input> box

    I have an <input> box and i want to disable the apostrophe ( ' ) key, so
    when you press it, no character appears in the input box. All other keys
    should work ok.

    I can trap the keypress event using "onkeypress=myK eypressHandler( )" but,
    beyond that, I'm stuck. I forget how to detect what key was pressed or how
    to "null it out".

    I'm using IE6 and users will be IE5.0 upward ONLY (trust me on this, suffice
    to say it's not a website but an intranet application).

    Any ideas please?

    Thanks!
    Owen


  • Martin Honnen

    #2
    Re: trapping/preventing keypress in &lt;input&gt ; box



    owen wrote:
    [color=blue]
    > I have an <input> box and i want to disable the apostrophe ( ' ) key, so
    > when you press it, no character appears in the input box. All other keys
    > should work ok.[/color]

    <input type="text"
    onkeypress="ret urn event.keyCode != '\''.charCodeAt ();">

    But note that the right arrow key -> has the same keyCode as the '
    character so you will cancel that key too.

    --

    Martin Honnen

    Comment

    • Evertjan.

      #3
      Re: trapping/preventing keypress in &lt;input&gt ; box

      Martin Honnen wrote on 01 dec 2004 in comp.lang.javas cript:
      [color=blue]
      >
      >
      > owen wrote:
      >[color=green]
      >> I have an <input> box and i want to disable the apostrophe ( ' ) key,
      >> so when you press it, no character appears in the input box. All
      >> other keys should work ok.[/color]
      >
      > <input type="text"
      > onkeypress="ret urn event.keyCode != '\''.charCodeAt ();">
      >
      > But note that the right arrow key -> has the same keyCode as the '
      > character so you will cancel that key too.
      >[/color]

      Not overhere, here the arrow gives no event.keycode at all.


      <input type="text"
      onkeypress="win dow.status=even t.keyCode;
      return event.keyCode != '\''.charCodeAt ();">

      Comment

      • Grant Wagner

        #4
        Re: trapping/preventing keypress in &lt;input&gt ; box

        owen wrote:
        [color=blue]
        > I have an <input> box and i want to disable the apostrophe ( ' ) key, so
        > when you press it, no character appears in the input box. All other keys
        > should work ok.
        >
        > I can trap the keypress event using "onkeypress=myK eypressHandler( )" but,
        > beyond that, I'm stuck. I forget how to detect what key was pressed or how
        > to "null it out".
        >
        > I'm using IE6 and users will be IE5.0 upward ONLY (trust me on this, suffice
        > to say it's not a website but an intranet application).
        >
        > Any ideas please?
        >
        > Thanks!
        > Owen[/color]

        <form>
        <input type="text" name="yourInput " onkeypress="ret urn
        testForApostrop he(event);">
        </form>
        <script type="text/javascript">
        function testForApostrop he(e) { // KEYPRESS event
        var k;

        if (e && e.which) { // NS
        k = e.which;
        } else if (window.event && window.event.ke yCode) { // IE
        k = window.event.ke yCode;
        }

        return (!k || k != 39);
        }
        </script>

        Although your users may wonder if there is something wrong with their keyboard
        when they hit an apostrophe and nothing appears.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Martin Honnen

          #5
          Re: trapping/preventing keypress in &lt;input&gt ; box



          Evertjan. wrote:

          [color=blue][color=green]
          >>But note that the right arrow key -> has the same keyCode as the '
          >>character so you will cancel that key too.
          >>[/color]
          >
          >
          > Not overhere, here the arrow gives no event.keycode at all.[/color]

          Right, I forgot that IE has its very own way of firing the different key
          events for such keys, it fires no keypress but only a keydown for the
          arrow keys so an onkeypress handler doesn't fire for arrow keys and
          therefore doesn't run the risk of cancelling those.

          --

          Martin Honnen

          Comment

          Working...