Powered by Blogger.

Sunday, May 18, 2014

RegularExpression Validator example in Asp.Net

In this post we will discuss one example of RegularExpression validator. Also you can check out some articles on:

- Anonymous Types in C#.Net

- Sealed class and Sealed method in C#.Net

- Regular expression validator for image files in Asp.Net

Recently I came accross a situation where I need a validation like below. A field can contain only interger ot 5 or 8 digit. Means it should through validation message of user enters number of length 7 digit, 6 digit, 4 digit etc.

But If you want to validate only numeric values, then you can follow this article.

Below is the code:

<asp:TextBox runat="server" ID="txtNumber" />
        <asp:RegularExpressionValidator runat="server" ID="rexNumber" ControlToValidate="txtNumber"
            ValidationExpression="^[0-9]{5}$|^[0-9]{8}$" ErrorMessage="Please enter a 5 or 8 digit number!"
            ForeColor="Red" />

It will appear like below: