js
function checkgender(){
$("#gender-result").hide();
if (undefined == $("input[name='gender']:checked", "#contact-form").val()) {
$("#gender-result").html('gender requirements.').show();
$("#gender-result").css('color', 'red');
return false;
}else{
$("#gender-result").hide();
}
return true;
}
function checkpassword(){
var password_value = $("#reg-password").val();
if(password_value === ""){
//password emtpy
$("#password-result").html('Please make a password');
$("#password-result").css('color', 'red');
$('#reg-password').css('border-color', 'red');
}else if (password_value.length < 6){
//password is too shot 6 digi
$("#password-result").html('Your password is too short.');
$("#password-result").css('color', 'red');
$('#reg-password').css('border-color', 'red');
}else{
//password passed
$("#password-result").html('');
$('#reg-password').css('border-color', '#dfe0e6');
return true;
}
return false;
}
但是,如果我使用它来提交验证,为什么它不起作用
$(document).click(function() {
$('#contact-form').submit(function(){
event.preventDefault();
//validation
if(!checkgender() && !checkpassword()){
return false;
}else{
return true;
}
});
});
如果我提交其中一个功能正常工作,但是如果我将2放在一起并按Submit,即使其中一个为空,它也让我通过了验证,为什么呢?我在这里做错了什么?
如果其中任何一个条件失败,则需要返回false
$(document).click(function () {
$('#contact-form').submit(function () {
//validation
if (!checkgender() || !checkpassword()) {
return false;
} else {
return true;
}
});
});
或者
短一点
$(document).click(function () {
$('#contact-form').submit(function () {
//validation
return checkgender() && checkpassword();
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句