Jquery验证使用依赖和检查未填充

时间:2012-08-06 21:28:29

标签: javascript jquery jquery-validate

我知道如何检查输入是否已填充,但是,如何检查输入是否未填充?

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

如果current_email字段已填满,这将使new_email字段成为必需字段。但我想检查并查看该字段是否为空,然后需要它。理由:

如果存在此类数据,我将从数据库返回current_email的值。如果是,则存在现有电子邮件,并且用户不需要添加new_email。如果数据库中存在空值,则current_email字段中将没有任何内容,从而强制用户输入new_email。

现在依靠我已尝试过:

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

但那里也没有运气。那么我怎么能让它发挥作用呢?感谢。

1 个答案:

答案 0 :(得分:0)

通过上面的@UnLoCo:

“首先,你有一个右括号缺失的回报($('#current_email')。val()=='';”