Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pseudo class :invalid apply to an input AFTER submitting a form

I have created a form and decided to validate it with just HTML5 and some JS without any additional plugins. So all of my required inputs have required attribute.

Here is the CSS to make them look nice

input:invalid {
    border: 2px solid #c15f5f
}

It sets the border of the invalid inputs to red, even if they haven't been touched yet.

How to make input:invalid apply after clicking submit button, along with the error messages?

like image 965
Jakub Małojło Avatar asked Jan 12 '17 17:01

Jakub Małojło


People also ask

How do you make an input field invalid?

If you add a customValidity message to the field, it becomes invalid. When you set the message as an empty string, it becomes valid again (unless it is invalid because of other reasons). field. setCustomValidity("Invalid field."); will make the field invalid.

Which CSS class can be used to style an input that is invalid?

The :invalid selector allows you to select <input> elements that do not contain valid content, as determined by its type attribute. :invalid is defined in the CSS Selectors Level 3 spec as a “validity pseudo-selector”, meaning it is used to style interactive elements based on an evaluation of user input.

Which pseudo code for input below will be triggered when HTML5 validation fails?

The :valid and :invalid pseudo-classes are used to represent <input> elements whose content validates and fails to validate respectively according to the input's type setting.

What CSS selector would you use to create a style sheet rule for all invalid required form fields created with the input element?

The :invalid selector selects form elements with a value that does not validate according to the element's settings.


2 Answers

You can add a class to the form when the submit-button was clicked and adjust your CSS selector so it only matches the input fields in a submitted form:

document.getElementById("submitButton").addEventListener("click", function(){
	document.getElementById("testForm").className="submitted";
});
form.submitted input:invalid{
  border:1px solid red;
}
<form id="testForm">
<input type="text" required>
<input type="submit" value="submit" id="submitButton">
</form>
like image 175
Thomas Altmann Avatar answered Nov 07 '22 18:11

Thomas Altmann


Building on Thomas' solution and idlefinger's comment, I would suggest listening for the HTML5 invalid event, which fires prior to the submit event.

// save all form controls as variable
var inputs = document.getElementById("your-form").elements;

// Iterate over the form controls
for (i = 0; i < inputs.length; i++) {

  inputs[i].addEventListener("invalid", function(){
    document.getElementById("your-form").className="submitted";

  });
};
form input {
    background: #eee;
}

form.submitted input:invalid {
    background: #ff3b3b;
}
<form id="your-form">
    <input type="text" required>
    <input type="text">
    <input type="submit" value="submit">
</form>
like image 1
Luke Avatar answered Nov 07 '22 16:11

Luke