1

I am trying to validate the password. I am using Data Annotations in my ViewModels like below:

[RegularExpression("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[@$!%*#?&])[A-Za-z[0-9]@$!%*#?&]{8,}$",ErrorMessage="Password must contain atleast 1 number, 1 letter, and 1 special character.")]

When I try to register with the right format, it still gives me the error message.

Can someone please look at it and help me out?

Jonathon Chase
  • 9,396
  • 21
  • 39
Faizan Rahman
  • 59
  • 3
  • 7

2 Answers2

0

In your regex, you have

[A-Za-z[0-9]@$!%*#?&]

You don't need the inner square brackets for [0-9]. It should be 0-9 or \d.

Kevin Lee
  • 1,171
  • 12
  • 30
0

You can use this regular expression to validate content

([a-zA-Z]{1,})([@$!%*#?&]{1,})([0-9]{1,})

But I would use another data-annotation attribute to validate length. Then you will know if your ModelState is failing because of invalid characters or length, for example [StringLength]

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
  • That doesn't work properly... eg `#abc123` will fail because your regex demands that you have a letter before the special character. – Chris Nov 16 '18 at 20:04