简体   繁体   中英

Regular Expression Validation in MVC 3, Visual Studio 2010, .Net Framework 4.0

I am using MVC3 for my web application, I have used regular expression to validate email adress.

[RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", ErrorMessage = "Please enter valid emailId.")]

It is not working at all. While I am updating record using LINQ, as below

                details.UserName = profile.UserName;
                details.Password = profile.Password;
                _context.SaveChanges();

Its giving an error which describes Nothing (You may be already familiar with it)

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

I got rid of it when I found this . Problem was due to Email was not validated properly, however my regular expression was valid. I googled and tried this and this but problem was not yet resolved. Some good this happened when I found this ., but still if you see above article, it has one line as

PropertyInfo property = validationContext.ObjectType.GetProperty(PatternProperty);

which return null and validation still fails. Could anyone help me out why MVC3 don't validate regular expressions as Javascript and suggest best solution to my problem?

MODEL

  public class SystemUserMetaData
    {
    [Mail]
        [MailAddress]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,4}\.[0-9]{1,4}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "invalid.")]
        //[DynamicReqularExpression(PatternProperty=@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")]
        [DataType(DataType.EmailAddress)]
        public string EmailId { get; set; }
    }

thanks.

[Required(ErrorMessage = "E-mail must be entered")] [DataType(DataType.EmailAddress)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM