Thursday 18 March 2010

Asp.Net Dynamic Data – RegularExpressionAttribute

As you would expect this attribute allows you to apply validation to a field in the form of a regular expression, if the data entered doesn’t match the expression it isn’t valid.

RegularExpression(“Pattern”, “Error Message”, “Error Resource”, “Error Resource Type”)

The expression will be evaluated against the field value when the user attempts to insert or update the data and if it doesn’t match then the error message will be displayed.  Just like the Range attribute you are able to specify either specific text or a resource for an error message.

Simply decorate the field with the attribute as per usual:

[RegularExpression(@"^\d{1,7}$",

ErrorMessage = "Quantity must be a whole positive number.")]

public object Quantity { get; set; }

And then when a user tries to put text in the field they will see:

2010-03-17_2113

No comments:

Post a Comment