Answered by:
Restrict Login1.UserName to accept Numeric Values only

Question
-
User1769015664 posted
How do I Restrict Login1.UserName to accept Numeric Values only?
I've tried to add an Attribute using the following code but having trouble passing the parameters:
TextBox txtUserName = (TextBox)Login1.FindControl("UserName");
txtUserName.MaxLength = 15;
txtUserName.Attributes.Add("onkeydown", ????????Tuesday, May 19, 2020 10:55 AM
Answers
-
User-719153870 posted
Hi NJ2,
still accepts % in the textboxPlease provide the latest code you are testing with since this still cannot be repro.
Also please check my current demo:
aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> function NumericOnly(evt) { var iKeyCode = (evt.which) ? evt.which : evt.keyCode if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)) return false; return true; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"></asp:TextBox> <asp:Login ID="Login1" runat="server"></asp:Login> </div> </form> </body> </html>
.cs:
protected void Page_Load(object sender, EventArgs e) { TextBox txtUserName = (TextBox)Login1.FindControl("UserName"); txtUserName.MaxLength = 15; txtUserName.Attributes.Add("onkeypress", "return NumericOnly(event)"); }
result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2020 2:09 AM
All replies
-
User-719153870 posted
Hi NJ2,
Seems you are trying to use JS function to restrict the numeric value according to the onkeydown event.
Please check Validate at client to accept only numbers in a textbox using JavaScript, and you might need to know there's a keyCode for each of your keyboard key.
Here's a demo that should meet the requirement:
aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> function NumericOnly(evt) { var iKeyCode = (evt.which) ? evt.which : evt.keyCode if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)) return false; return true; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Login ID="Login1" runat="server"></asp:Login> </div> </form> </body> </html>
cs:
protected void Page_Load(object sender, EventArgs e) { TextBox txtUserName = (TextBox)Login1.FindControl("UserName"); txtUserName.MaxLength = 15; txtUserName.Attributes.Add("onkeydown", "return NumericOnly(event)"); }
Best Regard,
Yang Shen
Wednesday, May 20, 2020 2:27 AM -
User1769015664 posted
Thanks, but this code doesn't work. It accepts special characters like %^&*#@ and doesn't accept delete or backspace
I've also tried the following code which works better but still accepts some special characters
function NumericOnly(e) {
var x=e.which||e.keycode;
if((x>=48 && x<=57) || x==8 || (x>=35 && x<=40)|| x==46)
return true;
else
return false;
}Wednesday, May 20, 2020 6:42 AM -
User-719153870 posted
Hi NJ2,
It accepts special characters like %^&*#@Sorry for this negligence, please change the event from onkeydown to onkeypress and this will be fixed.
and doesn't accept delete or backspaceThis cannot be reproduced if you mean delete what has been entered.
Best Regard,
Yang Shen
Wednesday, May 20, 2020 8:14 AM -
User1769015664 posted
still accepts % in the textbox
Wednesday, May 20, 2020 8:37 AM -
User-719153870 posted
Hi NJ2,
still accepts % in the textboxPlease provide the latest code you are testing with since this still cannot be repro.
Also please check my current demo:
aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> function NumericOnly(evt) { var iKeyCode = (evt.which) ? evt.which : evt.keyCode if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)) return false; return true; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"></asp:TextBox> <asp:Login ID="Login1" runat="server"></asp:Login> </div> </form> </body> </html>
.cs:
protected void Page_Load(object sender, EventArgs e) { TextBox txtUserName = (TextBox)Login1.FindControl("UserName"); txtUserName.MaxLength = 15; txtUserName.Attributes.Add("onkeypress", "return NumericOnly(event)"); }
result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2020 2:09 AM -
User1769015664 posted
Thanks, it worked.
Thursday, May 21, 2020 4:27 AM