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.

Community
  • 1
  • 1
SMI
  • 303
  • 1
  • 6
  • 22
  • [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Enter a valid email address.")] Try this. – Rifaj Jan 23 '14 at 06:09
  • 1
    Tried and failed..!! because the way javascript validate and MVC validates doesn't seems to be unique. I already tried some RegEx which was working fine with javascript but not MVC. – SMI Jan 23 '14 at 06:21
  • Could you please share the actual code – Andrei Jan 23 '14 at 06:28
  • What is the datatype that you are using for email field in db? – Rifaj Jan 23 '14 at 06:29
  • You already have an Email validator in data annotations. Are you trying to check for valid email id? – Rajshekar Reddy Jan 23 '14 at 06:31
  • @Reddy I think if I use .NetFramework 4.0, there will be no Email validators in Data.Annotaion. Is there any way to use that?? – SMI Jan 23 '14 at 06:33
  • Ensure Unobtrusive javascript validation is happening in your view. Check the second answer in this.. http://stackoverflow.com/questions/16430583/mvc-validation-regex-attribute-works-in-c-sharp-but-not-javascript – Rifaj Jan 23 '14 at 06:37
  • @Rifaj I am unable to search jquery.validate.unobtrusive.min.js – SMI Jan 23 '14 at 07:08
  • You mentioned that the error is due to unobtrusive validation. You are missing the email validation in your view. Check this article to enable unobtrusive validation. http://www.codeproject.com/Articles/577937/A-Beginners-Tutorial-on-Validating-Model-Data-and or else you need to have javascript validation on submit – Rifaj Jan 23 '14 at 07:16
  • @Rifaj I followed you but same problem :( – SMI Jan 23 '14 at 07:17

1 Answers1

0

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

SMI
  • 303
  • 1
  • 6
  • 22
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59