RegularExpression Attribute & Compare Attribute – Session 40

RegularExpression-Attribute-Compare-Attribute-Session-40

In this session of ASP.NET 5.0 / Core tutorial, we will talk about RegularExpression Attribute and Compare Attribute as well. If you are not familiar with Regular Expression in C#, you can find more about it in Document part of Microsoft website which is accessible form here. Also, you can find common and useful regular expression patterns in Regular Expression Library website and use them in your code. Moreover, you can test your selected regular expression to check whether your requirement is meet or not through Rubular web tool.

What is Regular Expression in C#?

Regular expression is flexible, powerful, and efficient tools for processing text. Regular Expression in C# is a pattern that can check and parse whether the input text is matching with the given pattern or not. Normally, we call C# regular expression as C# Regex. Also, .NET framework has an engine that handle regular expression and matching the entered text.

RegularExpression Attribute in ASP.NET 5.0 / Core

As mentioned earlier, this attribute is checking and parsing entered text with our pattern. Thus, if the text is matched with the pattern, ModelStatus.IsValid return true, otherwise it returns false and the user receive Form Validation Error.

Also, this attribute is in System.ComponentModel.DataAnnotations namespace. Hence, we should add the namespace to our Model or ViewModel to use RegularExpression Attribute. This attribute needs the regular expression as a string input parameter. Moreover, we can customize the error message with ErrorMessage parameter same as other attributes.

We can add @ before regular expression block to avoid compile error when we use backslash \ within our regular expression.

As you can see, we decorated Amount and Comment properties with RegularExpression attribute and customized its error message.

Compare Attribute in ASP.NET 5.0 /Core

As the name of attribute implies, this attribute compare two properties with each other. As a result, if both values are exactly the same, ModelStatus.IsValid returns true; otherwise, it returns false and the user receives Form Validation Error. In most cases, this attribute is using for Confirm Password and Confirm Email in user registration forms. Also, this attribute is in System.ComponentModel.DataAnnotations namespace. Hence, we should add the namespace to our Model or ViewModel to use this Attribute.

We should use Compare Attribute for confirmation properties same as Confirm Password or Confirm Email property. Then as a string input parameter, we should use name of property, that would be compared with this property . Moreover, we can customize the error message with ErrorMessage parameter same as other attributes.

Just bear in mind, this attribute is case sensitive, thus, value of both properties should be exactly the same even in terms of small and capital characters.

As you can see above, we decorated ConfirmComment property with Compare attribute and it is compared with Comment property. Also, as we just added this property, we need to add its respective View.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube. Moreover, you can have access to list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments