I have the following form which is added to a page via ajax
<script>
$(document).ready(function() {
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.appendTo('#invalid_' + element.attr('id'));
}
});
var validator = $("#login_form").validate({
rules: {
user_email: {
required: true,
email: true
},
password: {
required: true
}
}
});
});
</script>
<form id="login_form">
<center><br/><br/><br/>
<div class="login_div">
<center><h3>Log In</h3>
<table align="center" width="400px">
<tr><td class="input_td">Email:</td><td class="textfield_td"><input type="text" class="textfield" id="user_email" name="user_email"/></td></tr>
<tr><td colspan="2" id="invalid_user_email"></td></tr>
<tr><td class="input_td">Password:</td><td class="textfield_td"><input type="password" class="textfield" id="password" name="password"/></td></tr>
<tr><td colspan="2" id="invalid_password"></td></tr>
<tr><td colspan="2" class="error_text" id="login_error"></td></tr>
</table>
<input type="button" onclick="check_login();" value="Log In"/></center>
</div>
</center>
</form>
The check_login
method has this structure: if($("#login_form").valid()) {do something} else {return;}
When i click the Log in button the first time the form validates correctly e.g. If fields are missing or an invalid email is supplied then the appropriate error messages are shown. However if i then click the log in button again the valid()
method returns true even if fields are missing or there is an invalid email and no error messages are shown.
thanks for any light you can shed on this
EDIT: This solution appears to work where login is the id of the button
$("#login").click(function() {
validator.resetForm();
if(validator.form()) {
alert('valid');
}
else {
return;
}
});