I tried to solve my problem for 6 hours.
I use the jQuery Validation plugin.
I have a form with a password input. If the password is wrong (an AJAX request tells me), another input has value 0. If this input has no value, or value 0, I would like my form not to be submitted.
But it doesn't work and I can't explain why.
Here is my HTML :
<form method="post" action="ajax/traitement.php" id="form-durabilite" class="col-lg-12">
<input type="text" id="switch" name="input_cache" value=""/>
<input type="password" name="signature" id="sign"/>
<input class="btn btn-lg btn-warning" type="submit"value="Envoyer" />
</form>
And my jQuery :
$('#form-durabilite' ).validate({
rules: {
signature: {
required:function(element) {
return $('#switch').val() == 0 || $('#switch').val() == ''
}
}
},
messages: {
signature: {
required:'Votre mot de passe est obligatoire'
}
}
});
I have no message, nothing, like my rule does not exist ! What did I do bad?
You can use regular expression like this instead of writing function inside rules :
This [$.validator.addMethod] is common method for check input value as per given regular expression
$.validator.addMethod("regx", function(value, element, regexpr) {
var re = new RegExp(regexpr);
var ret = re.test(value);
return ret;
});
$('#form-durabilite' ).validate({
rules: {
signature: {
required:true,
regx:"^[^0-9]{6}$" // Here you can give regular expression for validating you input values
}
},
messages: {
signature: {
required:'Votre mot de passe est obligatoire',
regx:'Le mot de passe n'est pas valide'
}
}
});
I hope it will help you.
Please see how to submit form only if validation is success .
Add in submitHandler parameter as well with a form.submit() . This will trigger form-submit only when the jQuery validate is entirely truthy.
Please try this out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.