I have a form, if checkbox is checked, I will allow user to change their password, otherwise do not allow the user to change their password.
How can I do it?
$(function() {
var passworderror = "Password should be alphanumeric, 1 uppercase, 1 lowercase and 6 to 20 characters long.";
$.validator.addMethod("pwcheckspechars", function(value) {
return /[!@#$£%^&*()_=\[\]{};':"\\|,.<>\/?+-]/.test(value)
}, passworderror);
$.validator.addMethod("pwchecklowercase", function(value) {
return /[a-z]/.test(value) // has a lowercase letter
}, passworderror);
$.validator.addMethod("pwcheckuppercase", function(value) {
return /[A-Z]/.test(value) // has an uppercase letter
}, passworderror);
$.validator.addMethod("pwchecknumber", function(value) {
return /\d/.test(value) // has a digit
}, passworderror);
$('.myform').validate({
rules: {
changepsw: {
required: true
},
newpassword: {
required: true
}
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" class="myform">
<input type="checkbox" id="changepsw" name="changepsw">
<br/>
<label for="newpassword">New Password</label>
<input type="text" name="newpassword" id="newpassword">
<br/>
<input type="submit" value="submit">
</form>