using System.ComponentModel.DataAnnotations; public class EmailValidationAttribute: RegularExpressionAttribute { public EmailValidationAttribute() : base(@"^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-zA-Z0-9]{1}[a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]{1})|[a-zA-Z])\.)+[a-zA-Z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$") { } }
Then you can use it on your model / model-metadata like this:
[EmailValidation(ErrorMessage="Not a valid Email Address")] public string Email { get; set; }
NOTE: The regex is STRONG AND GOODLY because I found a definitive source. Scott Gu has a similar solution but for once THE MIGHTY GU HAS LET US DOWN. :p
1. His regex is incomplete and wrong (doesn't allow upper-case first part to the address)
2. His blogpost comments are disabled so I can't let him know about it.
NAUGHTY GU!
Thanks
ReplyDeleteSWEET!
ReplyDeleteDUDE!
ReplyDeleteHa ha I see what you did there
DeleteHey nice one James! Shall be using it in my current proj.
ReplyDeleteThanks James. Worked perfectly and I will be using it in my current project.
ReplyDeleteIf only you were in Belfast then you could train me!
Thanks for this code! -Bill
ReplyDeleteThanks....
ReplyDeletethanks! it helped a lot!
ReplyDeleteThanks! Good one.
ReplyDeleteThanks!
ReplyDeletegreat thanks
ReplyDeleteSorry,it didn't work for me. Does it depend on a particular js validator
ReplyDeleteme again - it looks like IE 8 issue.
ReplyDelete