asp.net mvc - JQuery > Validation > If textbox empty is fine > If user gives an input, validate with regex -
i have weird scenario. have form current password, new password , confirm new password field.
- current password required.
- new password not required unless user wants change it, in case must meet regex requirements (
^(?=.*[0-9])(?=.*[a-z])(?=.*[a-z])(?=.*[!@#$&*\"]).{6,16}?$
). - confirm new password not required matches new password.
following mvc cshtml hookup.
<li> <label for="password" class="required">current password</label> @html.textboxfor(m => m.userinfodata.password, new { @type = "password", @class = "k-textbox", @placeholder = "current password", @validationmessage = "current password required!", @required = "required" }) </li> <li> <label for="newpassword" class="required">new password</label> @html.textboxfor(m => m.userinfodata.newpassword, new { @type = "password", @class = "k-textbox", @placeholder = "new password", @required = "required", @pattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[a-z])(?=.*[!@#$&*\"]).{6,16}?$", }) </li> <li> <label for="confirmnewpassword" class="required">confirm new password</label> @html.textboxfor(m => m.userinfodata.confirmnewpassword, new { @type = "password", @class = "k-textbox", @placeholder = "confirm new password", @verifypasswords = string.empty }) </li> <li class="confirm"> <button class="k-button k-primary" type="submit" id="submitsetingsdata">update details</button> </li>
the following jquery hookup kendo validation.
$("#settings").kendovalidator({ rules: { verifypasswords: function (input) { var result = true; if (input.is("[name=userinfodata.confirmnewpassword]")) { result = input.val() === $("#userinfodata_newpassword").val(); } return result; } }, messages: { verifypasswords: "new passwords not match!" } });
validations working fine can't make new password field optional. html5 pattern requires required
want make field optional, if user leaves blank wouldn't check validation if user gives input must comply regex. i've used ?
@ end of regex not making optional. possible solution or hint on kendo validation or html5 appreciated. thank you.
perhaps adding "or empty" regex:
^(?=.*[0-9])(?=.*[a-z])(?=.*[a-z])(?=.*[!@#$&*\"]).{6,16}?$|^$
the |
means or , ^$
bit validates against empty strings. tried in regex validator , returned positive when either left string empty or used pattern compatible regex specified.
Comments
Post a Comment