Quantcast
Channel: Blog – James Croft
Viewing all articles
Browse latest Browse all 63

Adding TextBox validation to your UWP application with WinUX

$
0
0

There have been many occasions when I’ve been building apps that I’ve required the need for a form. This is normally a login screen which has some required fields that have specific client validation logic, usually a password length with certain character exceptions.

This is where WinUX’s ValidatingTextBox comes to help with it’s easy to use validation rule sets which can be easily extended for your own needs.

Validation text box

How does the ValidatingTextBox work?

Like an ordinary TextBox, the ValidatingTextBox provides the same functionality but with auto-validating features that allow users to get immediate validation messages while they type.

As a developer, the state of validation is exposed as a property called IsInvalid which you can bind to. This value is updated after validating the value in the text box through a collection of validation rules that you can set in code or XAML, which bind to the ValidationRules property. There are also two additional properties that will make the ValidatingTextBox a required field with an overwritable message to display when no value is provided. These are exposed through the IsMandatory and MandatoryValidationMessage properties.

If you set the MaxLength property of the control, you will also get a handy counter in the top right corner showing how many characters you’ve used and how many are left.

Here’s an example of how you might use the control in your app for validating a URL.

As this is a templated control, you also have the ability to apply a different template than the default I’ve provided shown in the GIF at the top of this article.

Creating validation rules

So now you know how to use the ValidatingTextBox but here’s how you can extend it to your own needs.

As previously stated, the control has a property called ValidationRules. This is an object that contains a collection of ValidationRule objects which is a base class. If you need to write custom validation logic, extending ValidationRule is the way to do it.

There are some common standard validation rules included within the WinUX project and these are as follows with what they do:

– CharacterLimitValidationRule, validates a string value against a minimum and/or maximum character length. I recommend not using this rule if you’re setting the MaxLength on the control.

– DoubleValidationRule, validates a string value can be parsed to a double value.

– EmailValidationRule, validates a string value is a valid e-mail address using Regex.

– IntValidationRule, validates a string value can be parsed to an integer value.

– RegexValidationRule, validates a string value against a Regex pattern that you provide.

– UrlValidationRule, validates a string value is a valid URL using the Uri.IsWellFormedUriString method.

The control has been designed to be as extendable as possible so feel free to rip apart and customise it to your needs, or just use it out of the box. It’s a great control for adding validation into your application.

If you have any question on how this works or just a general question about WinUX, feel free to get in touch.

You can keep up-to-date with the progress of the WinUX project over on GitHub.


Viewing all articles
Browse latest Browse all 63

Trending Articles