Question

Registrazione is a checkbox. When is clicked i want that email1 email2 pass1 pass2 become required. How can i do that? This is the source

registrazione: {
                required: false
                // TODO: set email1 email2 pass1 pass2 required!!
            }
            email1: {
                email: "Email non valida"
            },
            email2: {
                equalTo: "Gli indirizzi non corrispondono"
            },
            pass1: {
                password_regex: "Inserire solo lettere e numeri",
                minlength: "La password deve essere almeno di 6 caratteri",
                maxlength: "Password di massimo 20 caratteri"
            },
            pass2: {
                equalTo: "Le password non corrispondono"
            }
Was it helpful?

Solution 2

You can use depends inside required (see the jQuery Validate Docs), like this:

required: {
  depends: function (element) {
    return $("#YourcheckboxId:checked")
  }
}

Snippet

registrazione: {
    required: false
    // TODO: set email1 email2 pass1 pass2 required!!
}
email1: {
    email: "Email non valida",
    required: {
        depends: function (element) {
            return $("#YourcheckboxId:checked")
        }
    }
},
email2: {
    equalTo: "Gli indirizzi non corrispondono",
    required: {
        depends: function (element) {
            return $("#YourcheckboxId:checked")
        }
    }
},
pass1: {
    password_regex: "Inserire solo lettere e numeri",
    minlength: "La password deve essere almeno di 6 caratteri",
    maxlength: "Password di massimo 20 caratteri",
    required: {
        depends: function (element) {
            return $("#YourcheckboxId:checked")
        }
    }
},
pass2: {
    equalTo: "Le password non corrispondono",
    required: {
        depends: function (element) {
            return $("#YourcheckboxId:checked")
        }
    }
}

OTHER TIPS

Just add required attr to the field it will automatically validate the form

 $('#YourcheckboxId').on('click',function(){
if($this).is(': checked ')
{
 $('#email1, #email2, #pass1, #pass2 ').attr('required', 'required')
} else {
    $('#email1,#email2,#pass1,#pass2').removeAttr('required')
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top