jQuery提交验证不起作用

用户名

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,即使其中一个为空,它也让我通过了验证,为什么呢?我在这里做错了什么?

阿伦·P·约翰尼(Arun P Johny)

如果其中任何一个条件失败,则需要返回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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery提交验证帖子

来自分类Dev

jQuery表单验证提交处理程序不起作用

来自分类Dev

使用Jquery验证插件的Ajax表单提交不起作用

来自分类Dev

jQuery验证插件ajax提交不起作用

来自分类Dev

jQuery提交按钮提交不起作用

来自分类Dev

jQuery提交按钮提交不起作用

来自分类Dev

jQuery验证不起作用

来自分类Dev

jQuery验证不起作用

来自分类Dev

jQuery验证不起作用

来自分类Dev

jQuery验证不起作用?

来自分类Dev

jQuery验证不起作用

来自分类Dev

通过fancybox modal和jquery表单提交验证

来自分类Dev

jQuery 101表单提交不起作用

来自分类Dev

jQuery表单提交不起作用

来自分类Dev

jQuery .html的提交按钮不起作用

来自分类Dev

jQuery不起作用提交按钮

来自分类Dev

jQuery Ajax表单提交不起作用

来自分类Dev

jQuery克隆表单提交不起作用

来自分类Dev

jQuery提交表单不起作用

来自分类Dev

jQuery通过Ajax提交不起作用

来自分类Dev

表单提交与jQuery不起作用

来自分类Dev

提交时的 jQuery 不起作用

来自分类Dev

带有ajax提交处理程序的jquery验证插件不起作用

来自分类Dev

jQuery验证引擎不起作用

来自分类Dev

jQuery:表单验证不起作用

来自分类Dev

动态表单的jQuery验证不起作用

来自分类Dev

jQuery验证器addMethod不起作用

来自分类Dev

jQuery验证addMethod不起作用

来自分类Dev

jQuery表单验证不起作用?