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.
Member
75 Points
466 Posts
Restrict Login1.UserName to accept Numeric Values only
May 19, 2020 10:55 AM|NJ2|LINK
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", ????????
Contributor
3140 Points
983 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 20, 2020 02:27 AM|Yang Shen|LINK
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
Member
75 Points
466 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 20, 2020 06:42 AM|NJ2|LINK
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;
}
Contributor
3140 Points
983 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 20, 2020 08:14 AM|Yang Shen|LINK
Hi NJ2,
Sorry for this negligence, please change the event from onkeydown to onkeypress and this will be fixed.
This cannot be reproduced if you mean delete what has been entered.
Best Regard,
Yang Shen
Member
75 Points
466 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 20, 2020 08:37 AM|NJ2|LINK
still accepts % in the textbox
Contributor
3140 Points
983 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 21, 2020 02:09 AM|Yang Shen|LINK
Hi NJ2,
Please 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
Member
75 Points
466 Posts
Re: Restrict Login1.UserName to accept Numeric Values only
May 21, 2020 04:27 AM|NJ2|LINK
Thanks, it worked.