在验证失败时停止表单提交

时间:2018-01-12 06:10:25

标签: javascript php jquery validation laravel-5.2

我正在尝试验证表单的输入字段,但我遇到的问题是,当验证失败时,无论如何都会提交表单。我希望在验证通过之前阻止提交。

<form method="post" action = "withdrawSubmit" id="withdraw_form">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">

  <div class="form-group row">
    <div class="col-sm-12">
      <label class="col-sm-2 text-right">Address:</label>
      <input type="text" name="address" class="col-sm-10" id="address" value="">
    </div>
  </div>

  <div class="form-group row">
    <div class="col-sm-12" >
      <label class="col-sm-2 text-right">Amount:</label>
      <input type="number" step="any" name="amount" onchange="validateAmount()" class="col-sm-10" id="amount" value="">
      <span id="err_msg" style="color: red;"></span>
    </div>
  </div>
  <button type="submit" class="btn btn_wthdraw btn-primary " id="btn" >Withdraw</button>
</form>

validateAmount方法:

 function validateAmount(){
    var amount = document.getElementById("amount").value;
    var validationFailed = false;
    $.ajax({
          url: "{{url('getAmount')}}", 
          type: "GET",
          dataType: "json",
          success: function(result){
              var res = parseInt(result.BTC);
              if(amount > res)
              {
                $("#err_msg").html('Number should be less than to available amount');
                return false;
              }else{
                $("#err_msg").html(' ');
                return true;
              }

          }
    });
}

请帮助我得到这个。任何帮助将不胜感激。 提前谢谢。

0 个答案:

没有答案