Validate all fields at once with HTML5 instead of one at a time

Is there a way to make HTML5 check all fields at once, and not one at a time?

Using multiple JavaScript lines is fine, but I really don't want to use the whole module for this.

I searched quite a lot, but could not find a clean solution for this.

Here is an example form:

<html> <head> <title>Validation Test</title> <link href="css/style.css" rel="stylesheet"> </head> <body> <form action="" id="myForm" method="post"> <input id="name" name="name" required type="text"> <input id="phone" name="phone" required type="text"> <input type="submit" value="Submit"> </form> </body> </html> 

Css file:

 input:focus:required:invalid { border: 2px solid red !important; } 

The message proposed as a possible duplicate is that there are several validation conditions for one field, I want to check / show all invalid fields at the same time.

+8
css html5 validation
source share
2 answers

If you do not want to use jQuery, this is normal. You can easily do this with vanilla js event handlers and DOM selectors. I use jQuery to save time.

 $('#myForm').on('submit', function() { var $inputs = $(this).find(':input'), isValid = true; $inputs.each(function() { if ($(this).val() == '') { isValid = false; $(this).addClass('error-control'); } else { $(this).removeClass('error-control'); } }); return isValid }); 
+1
source share

Use onSubmit in the Form tag:

See: http://www.w3schools.com/js/js_validation.asp

  <form action="" id="myForm" method="post" onsubmit="return validateForm()"> 

Js:

 function validateForm() { // validate form here // return false if there are errors // return true to submit the form } 
0
source share

All Articles