Bootstrap form validation - form validation using on click event

I use bootstrap form validation and I want to validate my form with a button that is not inside the form tags.

The reasons why I want to do this:

  • I do not want to redirect the user to another page or reload the current page after submitting the form.

  • I want to call an ajax call that registers the user and returns a response (fail / success).

How can I call the validation form mechanism and get true or false to find out if I can redirect the ajax call.

Thanks.

+4
source share
2 answers

Just attach the jquery click event to the button outside the form tags and inside this function follow these steps:

//1) Submit Form using Form ID $("#testForm").submit(); //2 Submit Form using Form Name $("form[name='myForm']").submit(); //3 Submit Form using Form Index. $("form:first").submit(); 
+1
source

You can check your field value the way you want. This is just basic form submission code in jquery.

  $("#yourformid" ).submit(function( event ) { var field1 = $('#field1').val(); var field2 = $('#field2').val(); var information = { 'field1' : field1, 'field2':field2, }; var data = JSON.stringify(information); event.preventDefault(); $.ajax({ type: "POST", data: {value:data}, url: "YOUR_URL", success: function(data) { if(data="sucess"){ console.log(data) } else { console.log("Mission failed") } }); }); 
0
source

All Articles