"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by tomataus on Nov 11, 2010 10:14 PM
Use nuget to install MVC2(or 3) futures project. You can use the EmailAddressAttribute. It's also a good reference for how to extend it (and unobtrusively) if you need to.
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 05:43 AM|LINK
Its been so long since I did it for the first time.
Well if that's the case, heres some code, note that I work with jQuery validate so you might need to do even more to get this working on the client:
public class DataTypeValidator : DataAnnotationsModelValidator<DataTypeAttribute> { public DataTypeValidator(ModelMetadata metadata, ControllerContext context, DataTypeAttribute attribute) : base(metadata, context, attribute) { this.message = attribute.ErrorMessage; } string message; public override IEnumerable<ModelValidationResult> Validate(object container) { //TODO: Add email and url regularexpression validations. return base.Validate(container); } public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { List<ModelClientValidationRule> rules = new List<ModelClientValidationRule>(); ModelClientValidationRule rule; switch (Attribute.DataType) { case DataType.EmailAddress : rule = new ModelClientValidationRule() { ErrorMessage = message, ValidationType = "email" }; rule.ValidationParameters.Add("email", "true"); rules.Add(rule); break; case DataType.Url : rule = new ModelClientValidationRule() { ErrorMessage = message, ValidationType = "url" }; rule.ValidationParameters.Add("url", "true"); rules.Add(rule); break; } return rules; } }And add that RegisterAdapter line of code from my earlier post to your application_start
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 06:13 AM|LINK
This code is not buitin to MVC by default.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
wes-stueve
Member
2 Points
1 Post
Re: DataType.EmailAddress not being Validated
Jun 12, 2011 08:32 AM|LINK
Use nuget to install MVC2(or 3) futures project. You can use the EmailAddressAttribute. It's also a good reference for how to extend it (and unobtrusively) if you need to.
Data annotations Email Address