Validate form before submitting jquery

I have an html form. I validate my form using jquery.validate.js and is working on submitting an event. I want to confirm this form before submitting a request.

According to my R&D, I tried:

$('#my-form').on('before-submit',function(){ alert('Before submit performed'); }); 

I want to confirm the form immediately before submitting the event. Can someone tell me how to do this?

Many thanks,

M

+5
source share
1 answer

You can simulate the default behavior of the jQuery plugin as follows:

Check if form valid; if you do not prevent the default action using preventDefault() :

 $(document) .on('click', 'form button[type=submit]', function(e) { var isValid = $(e.target).parents('form').isValid(); if(!isValid) { e.preventDefault(); //prevent the default action } }); 

EDIT : this is useful if you want to fine-tune your validation test, that is, test certain controls conditionally.

+18
source

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


All Articles