Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi,
      I want validation for text box in a text box enter only digits. I want code in Asp.net but in this keypress event not available.
How to get key press event?
Posted 19-Aug-10 7:48am
Edited 19-Aug-10 8:59am
Abhinav S380.2K
v2
Comments
Sandeep Mewara at 19-Aug-10 15:16pm
   
You Javascript to trap the event.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
 
You add key press event in the code behind file as below
 
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onkeypress", "javascript:return allownumbers(event);");
    }
and write the Javascript function as below in the aspx page
 
 function allownumbers(e)
   {
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var reg = new RegExp("[0-9.]")
    if (key == 8)
    {
     keychar = String.fromCharCode(key);
    }
    if (key == 13)
    {
     key=8;
     keychar = String.fromCharCode(key);
    }
    return reg.test(keychar);
   }
  Permalink  
Comments
Member 366901 at 29-Apr-13 21:36pm
   
this is a test
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
 
<asp:TextBox ID="TextBox1" onkeyup="Page_ClientValidate();" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Only numeric data allowed."
            Display="Dynamic" ValidationExpression="\d+" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
 
At each key press, this code tests whether the input is Numeric or not. If input is not numeric, validation message is shown.
  Permalink  
Comments
Sandeep Mewara at 19-Aug-10 15:16pm
   
Reason for my vote of 1
ASP:Textbox is a server control and it won't have a client side onkeyup event exposed.
Al-Farooque Shubho at 19-Aug-10 23:24pm
   
Well, you are wrong.
 
Ideally, the "onkeyup" attribute should be added in CodeBehind. But, it doesn't mean that, it can't be added in the markup. You can add it and it works quite fine.
 
I added it in markup so that, the question author can understand it easily.
 
Just run the code in an aspx page in Visual Studio and see what is the output. I'd request you to consider re-vote after seeing the output.
 
Thanks.
eebert at 18-Jul-12 10:32am
   
Works perfectly. This solves the problem of how to validate on each keystroke. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 19 Aug 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100