Ways of allowing only alpha-numeric characters in Textbox.

Rajesh_Kumar
Posted by Rajesh_Kumar under ASP.NET category on | Points: 40 | Views : 1017
1st and very simple way:
Using RegularExpressionValidator


<asp:TextBox runat="server" Id="txt_policy_code" />
<asp:RegularExpressionValidator runat="server" id="rex_policy_code" ControlToValidate="txt_policy_code" ValidationExpression="^[a-zA-Z0-9]$" ErrorMessage="Only Alpha-Numeric Allowed" />

2nd way:
Using Javascript:


function checkAlphaNumeric(e) 
{
if ((e.keyCode >= 48 && e.keyCode <= 57) ||
(e.keyCode >= 65 && e.keyCode <= 90) ||
(e.keyCode >= 97 && e.keyCode <= 122))
{
return true;
}

return false;
}

<asp:Textbox Id="txt_policy_code" runat="server" onKeyPress="return checkAlphaNumeric();"></asp:Textbox>

Comments or Responses

Login to post response