This.txtProductCode, " Invalid product code." ) Īs you can see, the RegexValidator is much easier to use than the RegexValidationRule, particularly when being used in XAML.
#REGEX VALIDATOR CODE#
In fact, you cannot instantiate the RegexValidator class because it is a static class.īelow is an example of the RegexValidator in use:Ĭopy Code // Establish the Text property's binding.
It exposes its functionality via attached properties, so you never have to create an instance of RegexValidator to use the service it provides. The RegexValidator is a static class whose sole job is to create RegexValidationRule objects and add them to the appropriate Binding’s ValidationRules. Through the magic of attached properties, it is possible to avoid the multiple nested elements in XAML, as seen in the previous section, just to add a RegexValidationRule to a Binding’s ValidationRules. The RegexValidator class provides a streamlined way of using regular expressions to validate the text of a TextBox. The next section of this article shows a much easier way to make use of the RegexValidationRule class. It would be much more convenient if it was not necessary to create multiple nested elements, just to add a rule to the ValidationRules collection of a Binding. This is all well and good, but it seems like a lot of effort just to add an instance of the RegexValidationRule to the Text property’s associated Binding in XAML. Refer to this page in the SDK for more information about these necessary nuisances. When writing a regular expression in a XAML file, be sure to keep in mind that XAML requires certain characters to be escaped, such as ampersands (& = &) and apostrophes ( ' = '). RegexOptions - allows you to modify the RegexOptions property of the Regex object used to validate the input value.ErrorMessage - stores the text to be used/displayed when validation fails (such as the message in a tooltip).RegexText - stores the regular expression used during validation.The RegexValidationRule class has three public properties that can be used to adjust its behavior: TxtProductCode.SetBinding( TextBox.TextProperty, binding )
Rule.RegexOptions = RegexOptions.IgnoreCase Rule.ErrorMessage = " Invalid product code." RegexValidationRule rule = new RegexValidationRule()
#REGEX VALIDATOR HOW TO#
The XAML below shows how to use this class:īinding.Path = new PropertyPath( " ProductCode" ) The RegexValidationRule is designed to validate the Text property of a TextBox. I implemented this by creating the RegexValidationRule class, which derives from ValidationRule, and executing a Regex in its Validate override. My goal was to create a reusable means of validating user input via regular expressions. When it comes time for the bound value to be validated, all of the ValidationRule-derived objects will be queried to see if the input value is valid. Instances of the ValidationRule-derived classes can be added to a Binding’s ValidationRules collection. Classes which derive from ValidationRule must override the Validate method to perform custom validation logic and return a value which indicates whether the input value is valid or not. The general idea behind input validation in WPF involves the abstract ValidationRule class. This page in the SDK is also very informative. If you are not yet familiar with validation in WPF, I highly recommend Paul Stovell’s excellent article on the subject. This article assumes that the reader is familiar with the fundamentals of WPF validation. WPF provides an approach to input validation which is very different from the way that validation was performed in WinForms and ASP.NET.
NET Framework v3.0, but should work properly in later builds of the framework, as well. This code was compiled and tested against the June 2006 CTP of. The technique presented herein provides two ways for a developer to validate the text of a TextBox via regular expressions: explicitly adding a ValidationRule-derived object to the ValidationRules of a Binding, or simply using attached properties of a static service provider class.
#REGEX VALIDATOR WINDOWS#
This article demonstrates how regular expressions can be used to validate user input in a Windows Presentation Foundation (WPF) application.