It is possible to call a ajax after form validation is done.I have a form that validation with javascript onsubmit. I want to form submit when javascript validation is done before not allowed to submit the form.
Here is my form
<form method="post" id="register-form" class="sky-form" onsubmit="return register_validate()">
<h1 id="acc_title">Create your Account</h1><br>
<fieldset>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="input"> <i class="icon-append icon-user"></i>
<input type="text" name="fname" id="fname" placeholder="First Name">
</label>
<span style="display:none; color:red;" id="fname_error" class="input-notification error png_bg">Please Enter First Name.</span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="input"> <i class="icon-append icon-user"></i>
<input type="text" name="lname" id="lname" placeholder="Last Name">
</label>
<span style="display:none; color:red;" id="lname_error" class="input-notification error png_bg">Please Enter Last Name.</span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="input"> <i class="icon-append icon-envelope-alt"></i>
<input type="email" name="user_email" id="user_email" placeholder="Email">
</label>
<span style="display:none; color:red;" id="email_error" class="input-notification error png_bg">Please Enter Email.</span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="input"> <i class="icon-append icon-phone"></i>
<input type="text" name="user_phone" id="user_phone" placeholder="Phone">
</label>
<span style="display:none; color:red;" id="phone_error" class="input-notification error png_bg">Please Enter Phone No.</span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label>Type:</label>
<input type="radio" name="package_type" id="package_type" value="2" checked> Individual
<input type="radio" name="package_type" id="package_type" value="3"> Company
</div>
<input type="hidden" name="package_id" id="package_id" value="1">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="checkbox"><input type="checkbox" name="checkbox-inline" ><i></i>Agree with <a href="#">Terms & Conditions</a></label>
</div>
</div>
</div>
</fieldset>
<footer>
<input type="submit" name="register" class="login loginmodal-submit navigator_button" value="Register" data-navigate_to="code_form" data-navigate_from="code">
</footer>
<div class="login-help" id="login-help">
Already have account? <a href="#">login here</a>
</div>
</form>
Here is my javascript code
<script type="text/javascript">
function register_validate(e)
{
e.preventDefault();
var check = false;
var fname=document.getElementById("fname");
var lname=document.getElementById("lname");
var email=document.getElementById("user_email");
var phone=document.getElementById("user_phone");
if(fname.value == "")
{
$('#fname_error').show();
fname.focus();
check = false;
}
else
{
$('#fname_error').hide();
}
if(lname.value == "")
{
$('#lname_error').show();
lname.focus();
check = false;
}
else
{
$('#lname_error').hide();
}
if(email.value == "")
{
$('#email_error').show();
email.focus();
check = false;
}
else
{
$('#email_error').hide();
}
if(phone.value == "")
{
$('#phone_error').show();
phone.focus();
check = false;
}
else
{
$('#phone_error').hide();
}
if(check==false){
return false;
}else if(check==true)
return true ;
$.ajax({
url: 'reg_submit.php',
type: 'POST',
data: $(form).serialize(),
success: function(response) {
alert(response);
}
});
}
I am confusing about to call a ajax after validation is done. Thanks in Advance
Start your function register_validate with:
function register_validate(e){
e.preventDefault()
}
This will stop the form from submitting on its own. Now you need call ajax on your own from the function. If you need more info on it, check https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX
Prevent the form from getting submitted.
....edited
function register_validate(e) {
e.preventDefault();
var check = false;
if(fname.value == "") {
$('#fname_error').show();
fname.focus();
} else {
$('#fname_error').hide();
check = true;
}
if( check ) {
$.ajax({});
}
}
Also you can initialise a flag say var flag = false;
and then flag = true
when entered input is valid and at the end. Also, remove the return statement from the form. Just call the function onsubmit="register_validate();"
Remove
return true; after validation success otherwise it will return from that step and ajax call will not execute.
Change From
if(check==false){
return false;
}else if(check==true)
return true ;
$.ajax({
url: 'reg_submit.php',
type: 'POST',
data: $(form).serialize(),
success: function(response) {
alert(response);
}
});
To
if(check==false){
return false;
}else if(check==true)
{
$.ajax({
url: 'reg_submit.php',
type: 'POST',
data: $(form).serialize(),
success: function(response) {
alert(response);
}
});
}
And also add e.preventDefault() at the start of function to prevent form submission
function register_validate(e){
e.preventDefault();
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.