javascript验证完成后由ajax提交表单

时间:2018-04-12 10:06:40

标签: javascript html ajax

在表单验证完成后可以调用ajax。我有一个使用javascript onsubmit进行验证的表单。我想在javascript验证完成之前形成提交,然后才允许提交表单。

这是我的表格

<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>

这是我的javascript代码

    <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);
                    }            
                });

  }

我很困惑在验证完成后调用ajax。在此先感谢

3 个答案:

答案 0 :(得分:0)

使用以下函数启动函数register_validate:

function register_validate(e){
  e.preventDefault()
}

这将阻止表单自行提交。现在你需要从函数中自己调用ajax。如果您需要更多信息,请查看https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX

答案 1 :(得分:0)

阻止表单提交。

...编辑

    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({});
    }

 }

此外,当输入的输入有效且结束时,您可以初始化标记var flag = false;然后flag = true。另外,从表单中删除return语句。只需调用函数onsubmit="register_validate();"

即可

答案 2 :(得分:-1)

删除

返回true;验证成功后,否则它将从该步骤返回,并且ajax调用将不会执行。

更改
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);
                    }            
                });

if(check==false){
      return false;
    }else if(check==true)
 {
     $.ajax({
                    url: 'reg_submit.php',
                    type: 'POST',
                    data: $(form).serialize(),
                    success: function(response) {
                        alert(response);
                    }            
                });
}

并在函数开头添加e.preventDefault()以防止表单提交

function register_validate(e){
     e.preventDefault();