Return to the menu   Select another DES Module

Demonstrates the RegexValidator

The RegexValidator tests a field’s textual value against a regular expression. It is one of the most flexible Validators as it can look at the patterns of text to determine if it matches. Regular expressions can be used to verify phone numbers, IP Addresses, ID numbers, URLs, and so much more.

The setup is easy. Assign the textbox to the ControlIDToEvaluate property and a regular expression to the Expression property.

Unfortunately, building regular expressions can be difficult and has a learning curve. To assist you, DES predefines a number of regular expressions that you can access in the Properties Editor for the Expression property in Design Mode or by opening the [Web app]\DES\des.config file to the <RegExPatterns> section.


Also look at regexlib and Mozilla's JavaScript Reference for assistance.

Also consider these properties:

  • CaseInsensitive – When true, the regular expression parser will perform a case insensitive match. When false, it will perform a case sensitive match. It defaults to true.
  • Multiline – When true, it changes the meaning of the regular expression symbols "^" and "$" so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. It defaults to false.
  • IgnoreBlankText – Determines how blank text is evaluated. When true, the condition cannot be evaluated. When false, the condition evaluates as failed (reports an error). It defaults to true.
  • GlobalStorageName – Use this property to improve speed. When your Expression property never changes, assign a name here and the expression will be compiled into a .net Regex object and cached so the compilation process doesn't happen on each page request. If you have several RegexValidators that use the same Expression value, use the same value of GlobalStorageName so that they all share the same Regex object. This further improves your application’s speed.
  • PrepareProperties – When using the GlobalStorageName and you run time consuming code to create the expression (like a database query), put your time consuming code in a method assigned to PrepareProperties. Your code will only be called if the expression is not yet cached. This increases the application speed by avoiding that time consuming code when it’s not needed.

Controls

Example 1

Enter any URL. This expression is used: ^(?:|http\://|https\://)?(\w+\@)?(www\.)?\w+(\.\w+)+(\:\d+)?



Example 2

Enter a US or Canadian postal code. This expression is used: ^((\d{5}-\d{4})|(\d{5})|([A-Za-z]\d[A-Za-z]\d[A-Za-z]\d))$
Uses GlobalStorageName="PostalCode" to compile it once and to share it with other threads hitting this validator and for other validators that use GlobalStorageName="PostalCode" throughout the site.




Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <h2>Example 1</h2> Enter any URL. This expression is used: ^(?:|http\://|https\://)?(\w+\@)?(www\.)?\w+(\.\w+)+(\:\d+)?<br/><br/> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <des:RegexValidator ID="RegExValidator1" runat="server" ControlIDToEvaluate="TextBox1" ErrorMessage="Illegal value. Must be a URL." Expression="^(?:|http\://|https\://)?(\w+\@)?(www\.)?\w+(\.\w+)+(\:\d+)?"> </des:RegexValidator> <br/> <br/> <h2>Example 2</h2> Enter a US or Canadian postal code. This expression is used: ^((\d{5}-\d{4})|(\d{5})|([A-Za-z]\d[A-Za-z]\d[A-Za-z]\d))$<br/> Uses <span class="PropertyName">GlobalStorageName</span>="PostalCode" to compile it once and to share it with other threads hitting this validator and for other validators that use <span class="PropertyName">GlobalStorageName</span>="PostalCode" throughout the site.<br/><br/> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <des:RegexValidator ID="RegExValidator2" runat="server" ControlIDToEvaluate="TextBox2" ErrorMessage="Illegal value. Must be a postal code for the US or Canada." GlobalStorageName="PostalCode" Expression="^((\d{5}-\d{4})|(\d{5})|([A-Za-z]\d[A-Za-z]\d[A-Za-z]\d))$"> </des:RegexValidator> <br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>

Return to the menu   Select another DES Module