-2

Before submitting my form with jQuery after using event.preventDefault(), I need to check if my form has some validation errors (empty required fields, wrong data in "email" input, etc.) before submitting it programmatically (which will bypass the HTML5 validation and create a useless reloading returning a Django form error).

How can I do that?

0

1 Answer 1

0

Well, it's fairly easy !

When submitting a form, some "invalid" meta datas are added to the falsly inputs. With this, we can use a neat little CSS selector to detect if we have some incorrect fields :

let invalid_fields = $("form :invalid");

We now have a list of invalid fields or not. The world is fairly well made so every array has a "length" property. We can check this to determine if we're safe to submit the form or not :

if(invalid_fields.length == 0){
     //Do some funny things
    $("form).submit();
}

If the length is greater than 0, we have an error. If not, everything is good :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.