Jquery validation doesn't stop

I tear off my hair here, trying to get jquery confirmation to play beautifully with my form.

Verification does not work. The form is simply sent to the page. I can briefly see validation error messages that appear before the page is submitted ...

My code is:

//HTML form
<form id="form_scheduleEvent" name="form_scheduleEvent">
  <label for="name">Name:</label><input class="short" type="text" name="name" id="name" />
  <label for="address">Address:</label><input type="text" name="address" id="address" />
  <label for="phone">Phone:</label><input type="text" name="phone" id="phone" />
  <label for="comments">Comments:</label><textarea name="comments" id="comments" /></textarea>
  <input type="submit" id="submitRequest" value="Add"/>
</form>


//jquery
//Validation rules
$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required"
}
});


$('#submitRequest').click(function(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  });

 return false;
});

I tried updating to the latest version of jquery and jquery.validation ....

Any help would be appreciated! Thank.

Update

//The validation is working, but I can't even get the alert to show....
$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required",
   submitHandler: function(){
     alert('test');
   }
 }
});
+3
source share
7 answers

Thanks to everyone for their reply.

I ended up wrapping the ajax part of my source code in the following code:

if($('#form_scheduleEvent').valid() == true){
  //Process ajax request...
}
return false;
+4
source

You must move your AJAX submission request to get fired using the submit callback in the validation plugin:

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    submitHandler: function(form) { DoSubmit(); }
}
});


function DoSubmit(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  }
+5

. , - :

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    success: function() {
      $.ajax({
        type: "POST",
        url: "common/ajax_event.php",
        data: formSerialized,
        timeout:3000
      }
    }
});
+2

, , click .

, !;)

+1
$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required"
   },
   submitHandler: function(){
     alert('test');
   }

});

.: -)

0

Source: https://habr.com/ru/post/1775230/


All Articles