restrict the user entering nonalphanumeric keys?

Discussion in 'ASP .Net' started by =?Utf-8?B?Qw==?=, Jun 13, 2007.

  1. I want to restrict the user entering nonalphanumeric keys using Javascript
    onKeypress.
    One exception is that I want to allow the user to enter spaces.

    How can I do this?

    Thanks in Advance.
     
    =?Utf-8?B?Qw==?=, Jun 13, 2007
    #1
    1. Advertising

  2. Howdy,

    Two variants (both restrict to alphanumeric or nonalphanumeric):


    <asp:TextBox runat="server"
    onKeyPress="return restrict(this, true);" ID="TextBox1" />
    <asp:TextBox runat="server"
    onKeyPress="return restrict(this, false);" ID="TextBox2" />

    <script type="text/javascript">
    function restrict(input, alphanumeric)
    {
    var regex = alphanumeric ? /[a-zA-Z0-9_ ]/ : /[^a-zA-Z0-9_ ]/;

    var code = event.which ? event.which : event.keyCode;

    return regex.test(String.fromCharCode(code));
    }
    </script>

    hope this helps

    --
    Milosz


    "C" wrote:

    > I want to restrict the user entering nonalphanumeric keys using Javascript
    > onKeypress.
    > One exception is that I want to allow the user to enter spaces.
    >
    > How can I do this?
    >
    > Thanks in Advance.
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 13, 2007
    #2
    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. Jaydeep
    Replies:
    3
    Views:
    1,057
    vedcyrus
    Jun 30, 2006
  2. =?Utf-8?B?c3A=?=

    How do i restrict entering non-numeric data

    =?Utf-8?B?c3A=?=, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    559
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 27, 2004
  3. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    587
    Mike Wahler
    Oct 29, 2004
  4. PW
    Replies:
    10
    Views:
    952
    Jonathan N. Little
    Jun 18, 2006
  5. Chris Carlen
    Replies:
    1
    Views:
    627
    Gabriel Genellina
    Sep 18, 2007
Loading...

Share This Page