簡體   English   中英

在ajax帖子上使用jQuery Form Validator驗證表單()

[英]Validating form using jQuery Form Validator on ajax post ()

我正在使用表單驗證器插件來驗證我的表單。

如果我不使用jQuery post方法提交數據,它將正常工作。 在使用jQuery post時,它不會在提交時驗證表單。

我的代碼是

<form action="UpdatedProfile" method="post" name="updateprofile" id="UpdatedProfile" class="form-horizontal">
<div class=" form-group ">
          <div class="col-lg-2">
                <label>First Name</label>
          </div>
          <div class="col-lg-4">
              <input type="text" name="fname" cssClass="form-control" data-validation="required" data-validation-error-msg="First Name is required"/>
        </div>
    </div>
   </form>

<script>
            $(document).ready(function () {
                $.validate();
                $(document).on('click', '#submit', function (event) {
                    event.preventDefault();
                    console.log($('#UpdatedBasicProfile').serialize());
                    $.post("Updated", $('#UpdatedProfile').serialize(), function (data)
                   {....});
          });
         });
  </script>

如何實現呢?

在表單中放置一個提交按鈕,並在js中定義驗證規則,例如

$('#frm_registration').validate({
    rules:
    {
        fname: required
    },
    messages:
    {
        fname : 'Please enter name'
    }
});

或者,如果按鈕屬於按鈕類型,則在單擊時啟動驗證,例如:

$('#btn').click(function(){
    $('#frm_registration').valid();     // will initiate the validation
})

如果單擊提交按鈕時驗證程序正在工作,則可以在按鈕上觸發單擊事件,而不是偵聽文檔中是否有任何單擊,並且目標是給定的按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM