2

The problem is as follows,

The regular expression validation message on my view is being shown when another validation message is present, this happens even when the string matches the expression and seems like it is being triggered by other validation messages.

View 

    [Required]
    [DataType(DataType.Password)]
    [RegularExpression(@"^(?=.{8})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)", ErrorMessage = "message")]
    [Display(Name = "New Password")]
    public string NewPassword { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Compare("NewPassword")]
    [Display(Name = "Confirmation Password")]
    public string NewPasswordConfim { get; set; }

In the above model I have my regular expression that should enforce a password with 1 lowercase, 1 uppercase and 1 numeric character, the other validation is comparing the password confirmation with the new password field.

The reg expression / val message works when the string is valid or the confirmation field matches the new password field but if the confirmation field does not match then both validation messages display.

Controler

    if (Regex.IsMatch(model.NewPassword, @"^(?=.{8})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)"))
            {
                if (model.NewPassword != model.NewPasswordConfim)
                {
                    return View("PasswordResetVerification", model);
                }
            }

In my controler I added a check to make sure that the reg expression is valid (as a test) and this check has shown me in cases where the string is a match to the expression but the confirmation password field does not match the new password field the reg expression error message is still displayed in error.

View

   <div class="form-group">
                <label class="col-md-2 control-label" for="NewPassword">New Password</label>
                <div class="col-md-4">
                    <input type="password" class="form-control" id="NewPassword" name="NewPassword" placeholder="Password" value="@Model.NewPassword" />
                    @Html.ValidationMessageFor(m => m.NewPassword)
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label" for="NewPasswordConfim">Confim New Password</label>
                <div class="col-md-4">
                    <input type="password" class="form-control" id="NewPasswordConfim" name="NewPasswordConfim" placeholder="Confim Password" value="@Model.NewPasswordConfim" />
                    @Html.ValidationMessageFor(m => m.NewPasswordConfim)
                </div>
            </div>

The result of a new password of qW345678 and a confirmation of qW3456789 (non mathcing)

1
  • 2
    what password you try enter? Commented Jan 3, 2014 at 15:51

1 Answer 1

1

I think there is some problem with your regex i checked it dnt work properly.. I tried it here

Try this regex ^(?=.*\d)(?=.*[A-Z]).{8,20}$ working example here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I changed my regex to the following ^(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,20}$'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.