The RegularExpressionValidator Control

RegularExpressionValidator Control"-->

only for RuBoard

The RegularExpressionValidator Control

Through the use of regular expressions, the RegularExpressionValidator gives you tremendous control over how data is entered in to input controls and in what format. Regular expressions are used to match text character patterns to actual character strings. They also can be used to edit, extract, replace, or delete substrings (strings within strings). In this section, I am going to concentrate on the pattern-matching aspects of regular expressions with regard to the RegularExpressionValidator .

The RegularExpressionValidator has one unique attribute, ValidationExpression . The value for this attribute is the regular expression to be used for validation. Listing 9.5 contains a code example illustrating how to use the RegularExpressionValidator . In this example, we created a Regular Expression used to verify e-mail address syntax as described in the beginning of this chapter.

Listing 9.5 Using the RegularExpressionValidator to Validate a Phone Number
 01: <html> 02:  <body style="font-size:10"> 03:   <form runat="server"> 04: 05:    <h3>RegularExpressionValidator</h3> 06: 07:    Email Address: 08:    <asp:TextBox id="txtEmail" runat="server" width="200"/> 09: 10:    <p> 11: 12:    <asp:RegularExpressionValidator 13:     runat="server" 14:     ControlToValidate="txtEmail" 15:     Display="Dynamic" 16:     ValidationExpression= "[\w\x2E\x2D]{ 2,} \x40{ 1} [\w\x2E\x2D]{ 2,} \x2E{ 1} graphics/ccc.gif [\w\x2E\x2D]{ 2,} " 17:    > 18:     * Email Address is formatted incorrectly Ex: donny.mack@dotnetjunkies.com 19:    </asp:RegularExpressionValidator> 20: 21:    <p> 22: 23:    <asp:Button id="SubmitButton" 24:     runat="server" 25:     Text="Submit" 26:    /> 27: 28:   </form> 29:  </body> 30: </html> 

The RegularExpressionValidator is located on lines 12-19 and is wired to the txtEmail TextBox located on line 8. We won't be going through the Regular Expression used as the ValidationExpression value, but essentially it assures that a valid e-mail address is entered. Figure 9.3 contains an illustration of the page after the URL extension is left off of an e-mail address.

Figure 9.3. E-mail is not valid because there isn't a URL extension.
graphics/09fig03.gif

The ability to match and validate character patterns is only inhibited by your imagination and knowledge of regular expression syntax. Each of the regular expressions I used in Listings 9.3 through 9.5 can be written in different ways. This book isn't about regular expressions, so I am not going into great detail about the syntax, but for more information on regular expressions, please see http://msdn.microsoft.com/library/dotnet/cpguide/cpconintroductiontoregularexpressions.htm.

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net