The Following example shows, how to use the Regular Expression Validator to validate the Textbox, it allows only numbers(integer values only).
I have a Textbox control called txtAge:
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
Here I can use the Regular Expression in 2 methods.
Method 1: (Client Side validation)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtAge"
ErrorMessage="Enter Valid Age"
EnableClientScript="true"
ValidationExpression="\d+">
</asp:RegularExpressionValidator>
Exp: EnableClientScript="true", represents the client side validation.
Method 2: (Server Side validation)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtAge"
ErrorMessage="Enter Valid Age"
ValidationGroup="validate"
ValidationExpression="\d+">
</asp:RegularExpressionValidator>
Exp: ValidationGroup="validate", it will validate the textbox when a server side event hits. Like button click.
For that you have to use the button with the same validation group.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="validate" />
From the above Regular Expression Validator, we have to mention the validation expression in ValidationExpression. Here I have used the Validation Expression as "\d+" which represents only numbers.
We can mention the Validation Expression as "\d{6}", it represents totally six digits should be entered in Age textbox. It's mainly used for Postal (Zip) code, age validation and etc.
I have a Textbox control called txtAge:
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
Here I can use the Regular Expression in 2 methods.
Method 1: (Client Side validation)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtAge"
ErrorMessage="Enter Valid Age"
EnableClientScript="true"
ValidationExpression="\d+">
</asp:RegularExpressionValidator>
Exp: EnableClientScript="true", represents the client side validation.
Method 2: (Server Side validation)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtAge"
ErrorMessage="Enter Valid Age"
ValidationGroup="validate"
ValidationExpression="\d+">
</asp:RegularExpressionValidator>
Exp: ValidationGroup="validate", it will validate the textbox when a server side event hits. Like button click.
For that you have to use the button with the same validation group.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="validate" />
From the above Regular Expression Validator, we have to mention the validation expression in ValidationExpression. Here I have used the Validation Expression as "\d+" which represents only numbers.
We can mention the Validation Expression as "\d{6}", it represents totally six digits should be entered in Age textbox. It's mainly used for Postal (Zip) code, age validation and etc.