Tuesday, May 28, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
-6
rated 0 times [  1] [ 7]  / answers: 1 / hits: 17567  / 13 Years ago, fri, may 27, 2011, 12:00:00

jQuery Validation makes it simple to validate an email address:



$(someForm).validate({
rules: {
SomeField: {
required: true,
email: true,
remote: {
type: POST,
url: CheckEmail
}
}
}
});


This makes it so that SomeField is required, must be formatted as an e-mail address and also performs a remote call to the CheckEmail action (check for duplicates).



I like to make things as simple as possible so I can do a lot of the same stuff with Data Annotations:



public class RegisterModel {
[Required]
[Remote(CheckEmail, Home, HttpMethod=POST)]
public string SomeField { get; set; }
}


Does ASP.net MVC 3 / Data Annotations have a built-in/simple way to validate to make sure the e-mail address is in the correct format?



I would like it to produce unobtrusive javascript if possible.


More From » c#

 Answers
43

Does ASP.net MVC 3 / Data Annotations
have a built-in/simple way to validate
to make sure the e-mail address is in
the correct format?




Not built-in but you could use a [RegularExpression]. Scott Gu illustrated an example of such regex in a blog post. He wrote a custom EmailAttribute deriving from RegularExpressionAttribute to avoid repeating logic.


[#92020] Wednesday, May 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;