Reputation: 359
I have the following registration form :
<form id="register-form" method="post" role="form" style="display: none;"
action="">
<div class="form-group">
<input type="text" name="firstname" id="firstname" tabindex="1" class="form-control" placeholder="Firstname" value="" required>
<span class="error" id="fnError"></span>
</div>
<div class="form-group">
<input type="text" name="lastname" id="lastname" tabindex="1" class="form-control" placeholder="Lastname" value=""required>
<span class="error" id="lnError"></span>
</div>
<div class="form-group">
<input type="email" name="email2" id="email2" tabindex="1" class="form-control" placeholder="Email Address" value=""required>
</div>
<div class="form-group">
<input type="password" name="password2" id="password2" tabindex="2" class="form-control" placeholder="Password"required>
<span class="error" id="pass2Error"></span>
</div>
<div class="form-group">
<input type="password" name="confirm_password" id="confirm-password" tabindex="2" class="form-control" placeholder="Confirm Password" required>
<span class="error" id="confError"></span>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
<span class="throw_error" id="success"></span>
<div id="divLoader"></div>
</div>
<div class="col-sm-6 col-sm-offset-3">
<span id="result"></span>
</div>
</div>
</div>
</form>
And I am using the jquery validation plugin to validate the inputs :
jQuery.validator.setDefaults({
debug: false,
success: "valid"
});
$('#register-form').validate({
rules:{
firstname :{
required : true,
minlength : 3,
maxlength : 15
},
lastname :{
required : true,
minlength : 3,
maxlength : 15
},
email2:{
required : true,
email : true
},
password2:{
required : true,
minlength : 8,
maxlength : 15
},
confirm_password:{
required : true,
minlength : 8,
maxlength : 15,
equalTo : "#password2"
}
}
});
When I click on the registration button, the validation of the fields is shown (all the errors on each field are shown), and the ajax code that I am using on the button press runs even if there are validation errors. Here is my button clicked code :
$('#register-form').submit(function(event){
var formData={
'fn':$('#firstname').val(),
'ln':$('#lastname').val(),
'email':$('#email2').val(),
'pass':$('#password2').val(),
'action':'regUser'
};
$.ajax({
type: 'POST',
url: 'Index.php',
data: formData,
dataType: 'json',
beforeSend: function () {
$('#register-submit').attr("disabled","disabled");
$("#divLoader").addClass("loader");
console.log('before send');
},
success: function (data) {
if(data.success){
$('#success').fadeIn(1000).append('<p>'+data.userAdded+'</p>');
}
else{
$('.throw_error').fadeIn(1000).html(data.errors[0]);
}
},
error: function(){
alert("System currently unavailable, try again later");
},
complete: function () {
$('#register-submit').removeAttr('disabled');
$('#divLoader').removeClass('loader');
}
});
event.preventDefault();
});
What I want is that when I click on the submit button and there are validation problems that the button clicked event will not be triggered, so the AJAX call doesn't start.
Upvotes: 1
Views: 53
Reputation: 3614
try to name your validator
as variable
var myvalidator = $('#register-form').validate({
// ..
});
and in form submit add check like this:
$('#register-form').submit(function(event){
if (! myvalidator.valid() ) event.preventDefault();
// goes to ajax
});
Upvotes: 0
Reputation: 15603
You have to put the condition before the ajax call. You have to check the form validation with the function valid()
of jquery validate.
So your code will be like this:
$('#register-form').submit(function(event){
var formData={
'fn':$('#firstname').val(),
'ln':$('#lastname').val(),
'email':$('#email2').val(),
'pass':$('#password2').val(),
'action':'regUser'
};
if($(this).valid()) {
//ajax call will be inside of this if condition
}
Reference link.
And make the debug: true,
.
Upvotes: 1