submitted a value using ajax and jquery validation

I'm trying to check if the email value exist, so after validation work i use ajax to check on the email value. If the email exist, there should be an alert and form cannot be submitted. But the form keep submitted even if the email value exist in my table. Here's the script

$('#myForm').validate({
    rules: {

        email: {
            required: true,
            email: true
            }, 

        submitHandler : function(){

            var email = $("#email").val();
            $.ajax({ 
            url:"<?=base_url('akun/emailcheck');?>",
            type:"POST",
            dataType:"JSON",
            data:"email"+email,
            success: function(responeData){
                if(data==0){
                    alert('true');
                }
                else{
                    alert('false');
                }
            },
            error: function(data){
                    alert('error');
            }   
            });
        }
    }

Upvotes: 0

Views: 26

Answers (1)

adeneo
adeneo

Reputation: 318182

The submitHandler is not a rule, it's an option of it's own

$('#myForm').validate({
  rules: {
    email: {
      required: true,
      email: true
    }
  },
  submitHandler: function(form) {

    var email = $("#email").val();
    $.ajax({
      url: "<?=base_url('akun/emailcheck');?>",
      type: "POST",
      dataType: "JSON",
      data: {email : email},
      success: function(responseData) {
        if (responseData == 0) { // note that you're expecting JSON, not an integer ?
          alert('true');
          // form.submit();  <-- WILL SUBMIT FORM
        } else {
          alert('false');
        }
      },
      error: function(data) {
        alert('error');
      }
    });
  }
});

Upvotes: 1

Related Questions