views:

224

answers:

2

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?

A: 

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>
jessegavin
A: 

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