In this article I will explain with an example, how to use Regular Expression (Regex) to accept/allow only Alphanumeric characters i.e. Alphabets (Upper and Lower case) and Numbers (Digits) in TextBox in ASP.Net using:-
1. RegularExpression Validator.
2. JavaScript.
3. jQuery.
1. Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox using RegularExpression Validator
The following HTML Markup consists of a TextBox, a Button and a RegularExpression Validator which has been applied with the Regular Expression (Regex) to accept only Alphanumeric characters i.e. Alphabets (Upper and Lower case) and Numbers (Digits) as valid characters.
<asp:TextBox ID="TextBox1" runat="server" />
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ForeColor = "Red"
ValidationExpression="[a-zA-Z0-9]*$" ErrorMessage="*Valid characters: Alphabets and Numbers." />
<br />
<asp:Button ID="Button1" Text="Validate" runat="server" />
2. Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox using JavaScript
The following HTML Markup consists of a TextBox, a Button and an HTML SPAN. On the OnClientClick event of the Button, a JavaScript function is executed which validates the TextBox text against the Regular Expression (Regex) and if it contains character other than Alphabets or Numbers then an error message is displayed.
<asp:TextBox ID="TextBox2" runat="server" />
<br />
<span id="spnError" style="color: Red; display: none">*Valid characters: Alphabets and
Numbers.</span>
<br />
<asp:Button ID="Button2" Text="Validate" runat="server" CausesValidation="false"
OnClientClick="return Validate()" />
<script type="text/javascript">
function Validate() {
var isValid = false;
var regex = /^[a-zA-Z0-9]*$/;
isValid = regex.test(document.getElementById("<%=TextBox2.ClientID %>").value);
document.getElementById("spnError").style.display = !isValid ? "block" : "none";
return isValid;
}
</script>
3. Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox using jQuery
The following HTML Markup consists of a TextBox, a Button and an HTML SPAN. To the Button a jQuery click event handler is attached thus when the button is clicked, the TextBox text is validated against the Regular Expression (Regex) and if it contains character other than Alphabets or Numbers then an error message is displayed.
<asp:TextBox ID="TextBox3" runat="server" />
<br />
<span id="spnError2" style="color: Red; display: none">*Valid characters: Alphabets
and Numbers.</span>
<br />
<asp:Button ID="btnValidate" Text="Validate" runat="server" CausesValidation="false" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnValidate]").bind("click", function () {
var isValid = false;
var regex = /^[a-zA-Z0-9]*$/;
isValid = regex.test($("[id*=TextBox3]").val());
$("#spnError2").css("display", !isValid ? "block" : "none");
return isValid;
});
});
</script>
Screenshot
Demo
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.