I am writing an MVC application and I have a requirement for a strong password. Simple enough, I set up a view model with the following property.
[Required]
[RegularExpression(@"(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,", ErrorMessage ="1 upper, 1 lower, 1 digit, 8 character minimum length")]
public string PasswordEntered { get; set }
This works just fine when I use server validation: as long as I enter a lower an upper, a numeric and a total of 8 characters, my model is valid.
As soon as I turn on jquery unobtrusive validation, all my other fields validate without a round trip, but password stubbornly gives me my error message even if I enter a valid password like Password123.
Why does the regex fail to work? How do I correct this?
Edit: Further research. On debugging and stepping through jquery code I see this
return (match && (match.index === 0) && (match[0].length === value.length));
So because my regex does return any match groups, jquery considers the regex as having failed. I am not sure how to get a match group to satisfy jquery...
After a crash course in RegEx over 2 hours and going through jquery code I found that jquery requires the match group to be the same length as the input being tested. Since my original regex did not include a match group, the jquery function failed. I modified my original regex and it now works.
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(.{8,}$)