Divyesh Patoriya Divyesh Patoriya - 6 months ago 15
PHP Question

How to stop execution if validation is fire using jquery

Problem is there validation fire but submit form after submit button press.

Mean validation work properly, but after that form will submit which is wrong.

Here is my Code:

<script type="text/javascript">
$(document).ready(function(){
$('#updateevents').live('click',function(){
var evename=$("#Editname").val();
$.ajax({
type: 'POST',
url: 'middle/edited_save_event_data.php',
dataType: "json",
data: {name:evename},
success: function(data) {
$("#lightboxscheduleedit, #lightbox-panelscheduleedit").fadeOut(300);
}
});

});


Validation:

<script>
$(document).ready(function(){
$("#add").validate();
});
</script>


And This is Html:

<div id="lightbox-panelschedule">
<a id="close-panel" href="#"><img src="images/close-light.png"></a>
<h4 class="light_box_title">Add New Event</h4>
<div id="light_table">
<form name="addevent" id="add" method="post" action="add-trainer-event">
<div id="leftfirst">
<div class="main_login_form trainer_form">
<div class="post_form_row">
<label>Event Name:</label>
<input type="text" name="ename" class="required" id="ename">
</div>

</div>
</div>
</form>
</div>

<div class="clr"></div>



Answer

Try with the following code ..

$(function(){
    $("#add").validate();    
    $("#add").on('submit', function(e){
        var isvalidate=$("#add").validate();
        if(isvalidate){
            e.preventDefault();
          }
    });
});