简体   繁体   中英

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. 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. If so, there is an existing email and the user is NOT required to add a 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.

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:

"first, you have a closing parenthesis missing return ($('#current_email').val() == ''; "

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.

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