简体   繁体   English

jQuery使用Depends验证并检查未填充

[英]Jquery validate using depends and checking not filled

I get how to check that an input is filled, however, how do you check that an input is NOT filled? 我得到了如何检查输入是否已填充的方法,但是,如何检查输入是否未填充?

rules: {
    new_email:          {required: "current_email:filled"}
}

This will make the new_email field required if the current_email field is filled. 如果填写current_email字段,则将需要new_email字段。 But I want to check and see if the field is empty and make it required then. 但是我想检查一下该字段是否为空,然后将其设为必需。 The reasoning: 推理:

I'm returning the value of current_email from the database if such data exists. 如果存在这样的数据,我将从数据库中返回current_email的值。 If so, there is an existing email and the user is NOT required to add a new_email. 如果是这样,则存在现有的电子邮件,并且不需要用户添加new_email。 If there is a null value in the database, there will be nothing in the current_email field, thus forcing the user to enter new_email. 如果数据库中有一个空值,则current_email字段中将没有任何内容,从而迫使用户输入new_email。

Now with depends I've tried: 现在,依靠我已经尝试过:

rules: {
    new_email:{
               required: {
                   depends: function(element) {
                       return ($('#current_email').val() == '';
                   }
               }
              }
}

But there's no luck there either. 但是那里也没有运气。 So how would I get this to work? 那么我将如何使其工作呢? Thanks. 谢谢。

Via @UnLoCo above: 通过上方的@UnLoCo:

"first, you have a closing parenthesis missing return ($('#current_email').val() == ''; " “首先,您有一个右括号,缺少返回值($('#current_email')。val()==”;“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM