Watching multiple elements in a form, I would like to enable the submit button only when all elements successfully validate. Here's what I have attempted:-
$('#beta_signup_form').validate({
rules: {
name: {
required: true,
minlength: 4
},
email: {
required: true,
email: true
}
},
focusCleanup: true,
onkeyup: false,
errorElement: "span",
submitHandler: function(form) {
if ($('#beta_signup_form').valid()) {
console.log("now we enable the submit button!");
}
}
});
UPDATES
I was expecting the submitHandler
to be triggered and the if if ($('#beta_signup_form').valid())
condition to check that all our fields are valid, before we enable the submit button so that the user can click on it to make an ajax call to submit the form.
But somehow, nothing gets triggered when all fields are valid.
I figured out what I am getting wrong and that is the submitHandler
attribute has nothing to do with detecting the "all fields validated" event. The submitHandler
attribute only gets fired if I click on my form button, which is not what I need.
So my question is - which is the event I need to bind to? Is there an attribute that can be used for setting up a trigger when all fields in the form validate?
Here's the form html code:-
<form id="beta_signup_form" postUrl="{% url 'signups_beta_users' %}" method="post" csrf_token="{{ csrf_token }}">{% csrf_token %}
<div>
<input type="text" id="name" name="name" placeholder="Name" />
</div>
<div>
<input type="email" id="email" name="email" placeholder="Email address" />
</div>
<button class="btn btn-success" type="submit" id="request-trial" href="#request-for-free-trial">Notify Me</button>
<div class="loading"></div>
</form>