Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 6379 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > RegularExpressionValidator Control

RegularExpressionValidator Control

Article posted by Ganeshji on 7/25/2010 | Views: 4756 | Category: ASP.NET | Level: Beginner 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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:1 year(s)
Home page:
Member since:Saturday, July 24, 2010
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

Various options to convert Business Entity Class as XML

In this article we shall learn ASP.NET Dynamic data. With .NET 3.5, a new cool feature is added named as Dynamic Data which is a RAD development paradigm for building functional websites.

In this example i'll show how to detect the session timeout which occurs when user is idle for the time specified as Session.Timeout,using C# asp.NET and if it is than redirect the user to login page to login again, for this i've set time out value in web.config file to 1 minute

This article is a quick FAQ of Agile. By reading this you will understand fundamentals of Agile and different ways of implementing Agile.

I'm Going to explain now on some Control tip and tricks which will help and increase the speed of the webpages

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:14:53 AM