Part 83 - RegularExpression attribute in asp.net mvc
来源:互联网 发布:郑州做软件开发 编辑:程序博客网 时间:2024/05/17 22:22
Regular expression attribute is great for pattern matching validation. Let's understand using Regular expression attribute with an example. We will be continuing with the example that we started in Part 80 of the asp.net mvc tutorial.
Here is the requirement for validating Name property
1. Name can contain first and last name with a single space.
2. Last name is optional. If last name is not present, then there shouldn't be any space after the first name.
3. Only upper and lower case alphabets are allowed.
This requirement can be very easily met using RegularExpression attribute. In Employee.cs class file, decorate Name property with RegularExpression attribute.
[RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$")]
public string Name { get; set; }
Notice that, we are passing regular expression string to the attribute constructor. Regular expressions are great for pattern matching and ensures that, the value for name property is in the format that we want. Also, notice that we are using a verbatim literal(@ symbol) string, as we don't want escape sequences to be processed. We discussed verbatim literal in Part 4 of C# tutorial.
Understanding and writing regular expressions is beyond the scope of this video. If you are interested in learning to write regular expressions, here is a link from MSDN
http://msdn.microsoft.com/en-us/library/az24scfc.aspx
The following website is very helpful, for writing and testing regular expressions. This website also contains commonly used regular expressions. Infact, I have picked up the regular expression for validating Name property from here.
http://gskinner.com/RegExr/
Let's discuss another example of using validation attribute. A valid internet email address should have an @ and a DOT symbol in it. To match this pattern, use the following regular expression.
^[\w-\._\+%]+@(?:[\w-]+\.)+[\w]{2,6}$
In Employee.cs class file, decorate Email property with RegularExpression attribute as shown below.
[RegularExpression(@"^[\w-\._\+%]+@(?:[\w-]+\.)+[\w]{2,6}$", ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
- Part 83 - RegularExpression attribute in asp.net mvc
- Part 81 - Range attribute in asp.net mvc
- Part 82 - Creating custom validation attribute in asp.net mvc
- Part 84 - Compare attribute in asp.net mvc
- Part 60 - ViewStart in asp.net mvc
- Part 86 - ValidationSummary in asp.net mvc
- Authorize attribute and jquery AJAX in asp.net MVC
- Part 71 - childactiononly attribute in mvc
- Part 72 - HandleError attribute in mvc
- Part 2 - View engines in asp.net mvc
- Part 54 - T4 templates in asp.net mvc
- Part 85 - Enable client side validation in asp.net mvc
- Part 88 - Unobtrusive validation in asp.net mvc
- Part 89 - Remote validation in asp.net mvc
- ASP.NET MVC 4 Attribute特性
- ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 牛客网刷题之正则表达式匹配
- 成员函数调用
- 计算经纬度、距离、方位角
- QT Creator基础设置
- 394.Coins in a Line-硬币排成线(中等题)
- Part 83 - RegularExpression attribute in asp.net mvc
- android知识回顾-----单向链表
- Part 84 - Compare attribute in asp.net mvc
- android知识回顾-----开源框架-----Volley
- QT/C++ 下调用python自定义模块
- dom级别
- Part 85 - Enable client side validation in asp.net mvc
- java泛型
- HDU 1215 七夕节(筛选法)