Restrict user input

Discussion in 'Javascript' started by sconeek@gmail.com, Jan 18, 2006.

  1. Guest

    hi all,
    i have a textfield where i would like the user to input only Y or N.

    can somebody tell me how can i restrict the user from entering any
    other character, number or special character.
    thanks.
    , Jan 18, 2006
    #1
    1. Advertising

  2. McKirahan Guest

    <> wrote in message
    news:...
    > hi all,
    > i have a textfield where i would like the user to input only Y or N.
    >
    > can somebody tell me how can i restrict the user from entering any
    > other character, number or special character.
    > thanks.
    >


    Will this help? Watch for word-wrap.

    <html>
    <head>
    <title>YN.htm</title>
    <script type="text/javascript">
    function YN(that) {
    that.value = that.value.toUpperCase();
    var rex = /^[YN]$/;
    if (rex.test(that.value)) return;
    alert("Entry must be Y or N only.")
    that.value = "";
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="text" size="1"
    maxlength="1" onchange="YN(this)">
    </form>
    </body>
    </html>
    McKirahan, Jan 18, 2006
    #2
    1. Advertising

  3. Guest

    Also you can check the onkeypress event for the textbox, just delete
    the last charectar and pervious ones of similar type if their unicodes
    arent that of Y and N (on the event,keyCode varible, assuming function
    argument event)
    , Jan 18, 2006
    #3
  4. RobG Guest

    wrote:
    > hi all,
    > i have a textfield where i would like the user to input only Y or N.
    >
    > can somebody tell me how can i restrict the user from entering any
    > other character, number or special character.
    > thanks.
    >


    Why not use a checkbox or radio buttons instead?


    --
    Rob
    RobG, Jan 18, 2006
    #4
  5. Guest

    <html>
    <head>

    <script type="text/javascript">
    var returnvalue = false
    function keypro(event)
    {
    //this function returns true if y and false if n
    if (event.keyCode==89)
    {returnvalue = true}
    else if (event.keyCode==78)
    {returnvalue = false}
    else if (event.keyCode==8) //delete/backspace
    {}
    else
    {alert("Please only enter Y or N in the textbox!")}
    }

    </script>
    </head>
    <body>

    <form name="form1">
    <input type="text" onkeydown="keypro(event)" name="tf1">
    </form>

    </body>
    </html>


    thats an example of my idea
    , Jan 18, 2006
    #5
  6. RobG Guest

    wrote:
    > <html>
    > <head>
    >
    > <script type="text/javascript">
    > var returnvalue = false
    > function keypro(event)
    > {
    > //this function returns true if y and false if n
    > if (event.keyCode==89)
    > {returnvalue = true}
    > else if (event.keyCode==78)
    > {returnvalue = false}
    > else if (event.keyCode==8) //delete/backspace
    > {}
    > else
    > {alert("Please only enter Y or N in the textbox!")}
    > }


    If you need to use the actual key pressed, then:

    function keypro(event)
    {
    var x = event.keyCode || event.which;
    if ( 89==x || 78==x || 8==x) return;
    alert('Y or N please...');
    }


    is more concise but not recommended at all - it traps key presses like
    return, shift, alt, ctrl, etc. which is pretty awful. You will end up
    with a large number of ORs in there to let such represses go.

    Much better to test the actual text entered:

    function testInput(el)
    {
    var x = el.value;
    if (x == '' || /^[YyNn]$/.test(x)) return;
    el.value = '';
    alert('Only Y or N please...');
    }


    and use the keyup event:

    <input type="text" onkeyup="testInput(this);"
    maxlength="1" name="tf1">

    [...]
    >
    > thats an example of my idea


    Perhaps you should reply to the OP.


    --
    Rob
    RobG, Jan 18, 2006
    #6
  7. Guest

    great replies guys. its good to see that every problem has more than
    one solution and everybody is different.

    just another quick one, is there a list of keycodes and can somebody
    provide a link for it.

    thanks to all.
    , Jan 18, 2006
    #7
  8. RobG Guest

    wrote:
    > great replies guys. its good to see that every problem has more than
    > one solution and everybody is different.
    >
    > just another quick one, is there a list of keycodes and can somebody
    > provide a link for it.


    You can easily discover that using a simple script:

    <input type="text" onkeypress="showKeyCode(event, 'xx');">
    <div>keyCode: <span id="xx"></span></div>

    <script type="text/javascript">
    function showKeyCode(e, id)
    {
    var e = e || window.event;
    var x = e.keyCode || e.which;
    document.getElementById('xx').innerHTML = x;
    }

    </script>

    But it was suggested that you don't use keycode because you have a
    potentially unknown number of keycodes to deal with. You also want to
    deal with some of them differently, so there are at least 3 classes of
    code: acceptable, ignore and raise error.

    You also don't know if all browsers or user agents have the same
    keycodes for all their keys, e.g. Windows 'window' key, Mac OS Apple
    key, special function keys, etc.

    Using the entered text, you only have to deal with two cases:

    1. the value is one of Y, y, N, n or empty string so do nothing,
    2. anything else causes an action (error message, etc.)



    --
    Rob
    RobG, Jan 18, 2006
    #8
  9. Guest

    , Jan 18, 2006
    #9
  10. RobG Guest

    wrote:
    > Yeah I agree w/ Rob but I have found this to be useful:
    > http://www.w3schools.com/js/tryit.asp?filename=try_dom_event_keycode
    > press the key you wish to look up on it'll popup the keycode, also
    > shows you the source.


    You can use that if you like having to use your mouse to clear the alert
    boxes all the time, or you can use mine - no alerts to clear, see every
    key as you press it.

    Oh, and the code is right here. :)


    --
    Rob
    RobG, Jan 20, 2006
    #10
  11. Randy Webb Guest

    RobG said the following on 1/20/2006 1:13 AM:
    > wrote:
    >> Yeah I agree w/ Rob but I have found this to be useful:
    >> http://www.w3schools.com/js/tryit.asp?filename=try_dom_event_keycode
    >> press the key you wish to look up on it'll popup the keycode, also
    >> shows you the source.

    >
    > You can use that if you like having to use your mouse to clear the alert
    > boxes all the time,


    <spacebar> or <enter> key, no mouse needed to dismiss alerts.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jan 20, 2006
    #11
    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. Prasad
    Replies:
    1
    Views:
    626
    Kelly Leahy
    Jan 16, 2004
  2. Paras Wadehra
    Replies:
    1
    Views:
    1,779
    =?Utf-8?B?U2F1cmFiaCBOYW5kdQ==?=
    Aug 15, 2004
  3. Lawrance
    Replies:
    1
    Views:
    368
    Bhaskardeep Khaund
    Nov 30, 2003
  4. Jaydeep
    Replies:
    3
    Views:
    1,046
    vedcyrus
    Jun 30, 2006
  5. Tarkeshwar
    Replies:
    2
    Views:
    188
Loading...

Share This Page