ASP.NET RegularExpressionValidator is useful to allow user to enter only alpha numeric value and display error for any other special character found. The RegularExpressionValidator control belongs to System.Web.UI.WebControls
Syntax
<asp:RegularExpressionValidator ID="regularExp" runat="server"
ValidationExpression="^[a-zA-Z0-9 ]*$"
ErrorMessage="Invalid text, enter only 0-9 and a-z or A-Z character."></asp:RegularExpressionValidator>
Property | Description |
---|---|
ControlToValidate | Specifies control name which needs to be validate. |
Display | Specifies behavior of the error message. |
EnableClientScript | Specifies whether or not client-side validation is enabled. |
Enabled | Specifies whether or not validation control is enabled or not. |
ErrorMessage | Specifies error messageto be display in validation summary. |
ForeColor | Specifies error message color. |
IsValid | Indicate if associate control passes validation or not. |
SetFocusOnError | Set focus on associate control if validation fails. |
Text | Specifies text to be display if validation fails. |
ValidationGroup | Specifies validation group name. |
Validate | Update the isvalid propery. |
ValidateGetValidationProperty | Determineing validation property of a control, if it exists. |
ValidationExpression | Specifies regular expression that you want to match against input value. |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Alpha Numeric Validation Example</title>
</head>
<body>
<form id="Form" runat="server">
<div style="width: 619px">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium"
Font-Underline="True" Text="ASP.NET Alpha Numeric Validation Example"></asp:Label>
<table border="0" width="600px" cellpadding="0" cellspacing="0">
<tr><td>
<asp:Label ID="lblZip" runat="server" Font-Bold="True" Font-Size="Small"
Text="Enter Text:"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Width="177px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Invalid text, enter only 0-9 and a-z or A-Z character."
ValidationExpression="^[a-zA-Z0-9 ]*$"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox4" ErrorMessage="This required field, can not be blank."></asp:RequiredFieldValidator>
</td></tr>
<tr><td>
<asp:Button ID="btnSubmit" runat="server" Font-Bold="True"
Font-Size="Small" Text="Submit" Width="60px" />
</td></tr>
</table>
</div>
</form>
</body>
</html>
Above example will produce following output