开发者

How to enable Jquery form validation function to be executed/fired before other functions?

开发者 https://www.devze.com 2022-12-18 00:29 出处:网络
I use Jquery form validation to validate form input data. There is a confirm check on submit of this form.

I use Jquery form validation to validate form input data. There is a

confirm

check on

submit of this form. .

The code is:

<script type="text/javascript">
function Confirmation(){
    var answer = confirm("Do you really want to withdraw this amount of money from your account?")
    if (answer){
        return true;
    }
    else{
        return false;
    }

}
$(document).ready(function() { 
$("#withdraw").validate({ 
        rules: { 
         amount: {
            required: true,
             digits:true


        } ,
        bank:{
            required:true,

        },
        cardnumber1: {
            required: true,
             minlength:8

        },
       cardnumber2:{
          required开发者_开发百科:true,
          equalTo: "#cardnumber1"
         },
          holder:{
          required:true,
        }
  }
})
}); 
</script>

I want the Jquery form validation is executed before the Confirmation(), how to do it?


Steven,

You need to remove the onsubmit handler from the form and add it the the submitHandler of the validate plugin ...

$("#withdraw").validate({
        rules: {
           amount: {
            required: true,
             digits:true
                   }
             // .. other rules here
               },
        submitHandler: function(form){
         if( Confirmation() )
             form.submit();
  }
})


I looked at the documentation and there is an option named submitHandler which I think would allow you to add your confirmation dialog before the form is submitted but after the validation happens. Try this...

<script type="text/javascript">
$(document).ready(function() { 
  $("#withdraw").validate({ 
    submitHandler : function(form) {
      if (confirm("Do you really want to withdraw this amount of money from your account?")) {
        form.submit();
      }
    },
    rules: { 
      amount: {
        required: true,
        digits:true
      },
      bank:{
        required:true,
      },
      cardnumber1: {
        required: true,
        minlength:8
      },
      cardnumber2:{
        required:true,
        equalTo: "#cardnumber1"
      },
      holder:{
        required:true,
      }
    }
  })
}); 
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消