CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2006
    Location
    India
    Posts
    107

    Restricting Key Stokes in text box

    Hello

    I have an web application (asp.net + C#)

    I have an textbox that is always filled with numeric value.

    i want to restrict other all the key press except the numeric key & - .
    as for the phone the hyphen is required.

    How to implement it?

    I tried but no success.

    Any Idea?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Restricting Key Stokes in text box

    As pressing a key in a web-browser is a client side event, so you will have to use Client Side Javascript to do that.

    Here is a script that will allow only numbers in a textbox
    Code:
    	<SCRIPT language=javascript>      
          function isNumberKey(evt)
          {
             var charCode = (evt.which) ? evt.which : event.keyCode
             if (charCode > 45 && (charCode < 48 || charCode > 57))
                return false;
             return true;
          }      
       </SCRIPT>
    Remember in your HTML part of the TextBox you will have to write this onkeypress="return isNumberKey(event)"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured