K
K
kalaysolay2017-09-19 09:45:11
ASP.NET
kalaysolay, 2017-09-19 09:45:11

How to do password validation with regular expressions in ASP.NET MVC project?

I am learning asp.net mvc
There is a model:

public class LoginViewModel
    {
        [Display(Name = "Логин")]
        [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string Login { get; set; }

        [Display(Name = "Пароль")]
        [DataType(DataType.Password)]
        [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources.Resources))]
        [RegularExpression("/(?=.*[0-9])(?=.*[[email protected]#$%^&*])(?=.*[a-z])(?=.*[A-Z])[[email protected]#$%^&*]", ErrorMessageResourceName = "PasswordIsNotValid", ErrorMessageResourceType = typeof(Resources.Resources))]
        public string Password { get; set; }
    }

I put it on the page like this:
<div class="common-row">
    <label class="common-label">
       @Resources.NewPassword
    </label>
        @Html.TextBoxFor(model => model.Password, new { @class = "common-input", type = "password" })
    <div class="error__input">
        @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
   </div>
</div>

The rules for validation are:
1. Password must contain at least one uppercase letter
2. Password must contain at least one lowercase letter
3. Password must contain at least one special character
4. Password must contain at least one digit
Expression (?=.*[ 0-9])(?=.*[[email protected]#$%^&*])(?=.*[az])(?=.*[AZ])[[email protected]# $%^&*] checked for regex101 and everything seems to be working.
But, when I try to enter a password on the site, it swears that the password does not comply with the rules. I don't understand what I'm doing wrong

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question