RegularExpressionValidator Control

Ganeshji
Posted by in ASP.NET category on for Beginner level | Views : 15920 red flag

I have tried to use few of the properties of this control in my example.This will give a clear picture of the various benefits of this control.

Introduction



By using this RegularExpressionValidator control, you can check a user’s input based on a pattern that you define using a regular expression. This type of control allows you to check for predictable sequences of characters, such as those in email addresses, telephone numbers, postal codes, and so on.



Regular Expressions used in my example



Content

Regular Expression

Description

Password

\w+

Any sequence of one or more word characters.

Specific-Length Password

\w{4,10}

Password length must be at least 4 and max 10 characters.

Advanced Password

[a-zA-Z]\w{3,9}

The first char must be within the range of a-z or A-Z and its length must be at least 4 and max 10.

Limited-Length

\S{4,10}

It allows 4 to 10 characters, but it allows special characters.

Mobile Number

^([9]{1})([234789]{1})([0-9]{8})$

First digit must be 9. Second digit will be any 1 from (234789) and last 8 digits will be from 0-9.

Phone Number

\d{10}

Only 10 digits are allowed here.



Table 1: Regular Expressions used in my example

 


Properties used in my example



Property

Description

ControlToValidate

The ID of the form field being validated

ValidationExpression

ValidationExpression property is initialized with regular expression. It sets the pattern for comparison purpose.

EnableClientScript

Gets or sets a value indicating whether the RegularExpressionValidator control updates itself using client-side script.

Display

It is of 3 types:

None-RegularExpressionValidation control is not visible

Static- Space for error message is reserved irrespective of the non-occurrence of error.

Dynamic- Space for error message is not reserved in case of  the non-occurrence of error.

Table 2: Properties of RegularExpressionValidator control used in my example

Coding



 RegularExpression.aspx

 

<center>



            <table>



                <tr>



                    <td>



                        <asp:Label ID="Label1" runat="server" Text="Specific-Length Password"></asp:Label>



                    </td>



                    <td>



                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



                    </td>



                    <td>



                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ValidationExpression="\w{4,10}"



                            ControlToValidate="TextBox1"
Display="Static"
EnableClientScript="false"
runat="server"



                            ErrorMessage="Must be
the length between 4 to 10 chars"></
asp:RegularExpressionValidator>



                    </td>



                </tr>



                <tr>



                    <td>



                        <asp:Label ID="Label2" runat="server" Text="Advanced Password"></asp:Label>



                    </td>



                    <td>



                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>



                    </td>



                    <td>



                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2"
ValidationExpression="[a-zA-Z]\w{3,9}"



                            ControlToValidate="TextBox2"
Display="Static"
EnableClientScript="false"
runat="server"



                            ErrorMessage="Must start
with a to z (in both cases) and length between 3 to 9 chars"></
asp:RegularExpressionValidator>



                    </td>



                </tr>



                <tr>



                    <td>



                        <asp:Label ID="Label3" runat="server" Text="Limited Length"></asp:Label>



                    </td>



                    <td>



                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>



                    </td>



                    <td>



                        <asp:RegularExpressionValidator ID="RegularExpressionValidator3"
ValidationExpression="\S{4,10}"



                            ControlToValidate="TextBox3"
Display="Static"
EnableClientScript="false"
runat="server"



                            ErrorMessage="Special
Characters "></
asp:RegularExpressionValidator>



                    </td>



                </tr>



                <tr>



                    <td>



                        <asp:Label ID="Label4" runat="server" Text="Mobile Number"></asp:Label>



                    </td>



                    <td>



                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>



                    </td>



                    <td>



                        <asp:RegularExpressionValidator ID="RegularExpressionValidator4"
ValidationExpression="^([9]{1})([234789]{1})([0-9]{8})$"



                            ControlToValidate="TextBox4"
Display="Static"
EnableClientScript="false"
runat="server"



                            ErrorMessage="Mobile number must
start with 9"></
asp:RegularExpressionValidator>



                    </td>



                </tr>



                <tr>



                    <td>



                        <asp:Label ID="Label5" runat="server" Text="Phone Number"></asp:Label>



                    </td>



                    <td>



                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>



                    </td>



                    <td>



                        <asp:RegularExpressionValidator ID="RegularExpressionValidator5"
ValidationExpression="\d{10}"



                            ControlToValidate="TextBox5"
Display="Static"
EnableClientScript="false"
runat="server"



                            ErrorMessage="Phone
number must be of 10 digits"></
asp:RegularExpressionValidator>



                    </td>



                    <td>



                        <asp:Button ID="Button5" runat="server" Text="Validate" />



                    </td>



                </tr>



            </table>



        </center>


Output: Using wrong data




Successful Output:




Conclusion

Note: I haven’t used the Password Content (Refer: Table 1) in my example.

Page copy protected against web site content infringement by Copyscape

About the Author

Ganeshji
Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)