How to use the onsubmit event?

Hello!

Task:
When the form is submitted you need to check whether fields are filled and if any fields are filled incorrectly, you need to highlight invalid fields in red.

Code:
https://jsfiddle.net/822ewjbc/

I did a primitive test using the event handler "invalid", by clicking on the button or press enter the form is submitted. Then I decided to improve the code and read about onsubmit.
But as soon as I try to use it,lost a custom message that says that I'm doing something wrong.
June 10th 19 at 14:41
1 answer
June 10th 19 at 14:43
Solution
On Jupiter! Don't need no handlers!
Normal html form with a normal button sabmita, like in the good old 2000s))
In other words do carry js.
As for validation, use the parameter tag require. That's the whole business.
If you want, you can also use regexps Paterna, for compliance with not only length, but also the pattern (tel, mail...), in the parameter tag pattern.
And you need to use onsubmit for a more extended review of js-om before submitting the form, or if you want to send the form Ajax. Do not forget to cancel the default behavior using event.preventDefault();

$("form").on("submit", function(event){

event.preventDefault();

 var formData = $("form").serialize();

$.ajax({
 dataType: "html",
 url: "myUrl.php",
 cache: false,
 type: "POST",
 data: formData,
 success: function(msg){
console.log(msg);
},
 error:function(jqXHR, textStatus){
alert(textStatus);
}
});
 });
Thanks for the advice)
But,unfortunately,I am required to do it through js ;c - Edward82 commented on June 10th 19 at 14:46
well , then here:
$("form").on("submit", function(event){

event.preventDefault();

 var formData = $("form").serialize();

$.ajax({
 dataType: "html",
 url: "myUrl.php",
 cache: false,
 type: "POST",
 data: formData,
 success: function(msg){
console.log(msg);
},
 error:function(jqXHR, textStatus){
alert(textStatus);
}
});
 });
- Leda15 commented on June 10th 19 at 14:49
thank! - Edward82 commented on June 10th 19 at 14:52

Find more questions by tags JavaScript