TJ VanToll
An algorithm run whenever a user submits a form.
<form>
<input type="text">
<button>Go</button>
</form>
document.getElementById( "myForm" )
.addEventListener( "submit", function( event ) {
if ( document.getElementById( "myInput" ).value == "" ) {
event.preventDefault();
// Show some questionably UX and a11y friendly
// error message.
}
});
});
<form>
<input type="text" required>
<button>Go</button>
</form>
<form>
<input type="number" required
min="2" max="10">
<button>Go</button>
</form>
<form>
<input type="text" maxlength="2">
<button>Go</button>
</form>
Constraint validation: If an element has a maximum allowed value length, its dirty value flag is true, its value was last changed by a user edit (as opposed to a change made by a script), and the code-unit length of the element’s value is greater than the element’s maximum allowed value length, then the element is suffering from being too long.
<form>
<input type="text" maxlength="2"
value="1234">
<button>Go</button>
</form>
<form>
<input type="text"
pattern="[0-9].[0-9][0-9]">
<button>Go</button>
</form>
<form>
<input type="text"
title="In x.xx format"
pattern="[0-9].[0-9][0-9]">
<button>Go</button>
</form>
<form>
<input type="text"
pattern="(?:19|20)(?:(?:[13579][26]|[02468][048])-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))|(?:[0-9]{2}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:29|30))|(?:(?:0[13578]|1[02])-31)))">
<button>Go</button>
</form>
<form>
<input required>
<input type="checkbox" required>
<input type="radio" name="boxes" required>
<input type="radio" name="boxes" required>
<select required>
<option selected></option>
<option>One</option>
</select>
<textarea required></textarea>
<button>Go</button>
</form>
<form>
<input type="number" step="2">
<button>Go</button>
</form>
<form>
<input type="email" required>
<input type="url" required>
<button>Go</button>
</form>
<form>
<input type="date">
<button>Go</button>
</form>
Boolean attribute that can be applied to <form> elements to prevent validation.
<form novalidate>
<input type="text" required>
<button>Go</button>
</form>
Boolean attribute that can be applied to submit buttons to prevent validation.
<form>
<input type="text" required>
<button>Validate</button>
<button formnovalidate>
Do NOT Validate</button>
</form>
willValidate
validity
validationMessage
checkValidity()
reportValidity()
setCustomValidity()
Boolean property indicating whether a node is a candidate for constraint validation.
<div id="one"></div>
<input type="text" id="two">
<input type="text" id="three" disabled>
<script>
document.getElementById( "one" ).willValidate; //undefined
document.getElementById( "two" ).willValidate; //true
document.getElementById( "three" ).willValidate; //false
</script>
<form>
<input type="text" required>
<button>Go</button>
</form>
A ValidityState object with the following boolean properties:
The message the browser displays to the user when a node's validity is checked and fails.
<form>
<input type="text" id="one" required>
</form>
<script>
document.getElementById( "one" ).validationMessage;
// Chrome --> "Please fill out this field."
// Firefox --> "Please fill out this field."
// Safari --> "value missing"
// IE --> "This is a required field."
</script>
Method that determines the validity of <form> nodes and form elements.
<form id="form">
<input type="text" id="one" required>
<input type="text" id="two">
</form>
<script>
document.getElementById( "one" ).checkValidity(); // false
document.getElementById( "two" ).checkValidity(); // true
document.getElementById( "form" ).checkValidity(); // false
</script>
Same as checkValidity(), but show the bubble.
Not supported in any browsers yet.
Set validationMessage and create custom validation rules.
<form id="form">
<input type="text" id="input-2">
<button type="submit">Go</button>
</form>
<script>
var input = document.getElementById( "input-2" );
input.setCustomValidity( "Custom error message" );
</script>
<form id="form">
<input type="password" id="pass-1" required>
<input type="password" id="pass-2" required>
<button type="submit">Go</button>
</form>
<script>
var password1 = document.getElementById( "pass-1" ),
password2 = document.getElementById( "pass-2" );
function checkPasswordValidity() {
if ( password1.value != password2.value ) {
password1.setCustomValidity( "Passwords must match." );
} else {
password1.setCustomValidity( "" );
}
}
password1.addEventListener( "change", checkPasswordValidity );
password2.addEventListener( "change", checkPasswordValidity );
</script>
<form id="form">
<input type="text" id="input-1" required>
</form>
<script>
document.getElementById( "input-1" )
.addEventListener( "invalid", function(event) {
var text = document.createTextNode( "INVALID!" );
event.target.parentNode.appendChild( text );
});
document.getElementById( "input-1" ).checkValidity();
</script>
<form>
<input type="text" id="input-10" required>
<button>Go</button>
</form>
<script>
document.getElementById( "input-10" )
.addEventListener( "invalid", function(event) {
var text = document.createTextNode( "INVALID!" );
event.target.parentNode.appendChild( text );
});
</script>
<form>
<input type="text" id="test-1" required>
<input type="text" id="test-2" required>
<button>Go</button>
</form>
<script>
function background( event ) {
event.target.style.background = "red";
}
document.getElementById( "test-1" )
.addEventListener( "invalid", background );
document.getElementById( "test-2" )
.addEventListener( "invalid", background );
</script>
An invalid event on forms!
<form id="form-1">
<input type="text">
<input type="text" required>
</form>
<style>
#form-1 :required { background: red; }
#form-1 :optional { background: blue; }
</style>
<form id="form-2">
<input type="text" required value="Lorem Ipsum">
<input type="text" required>
</form>
<style>
#form-2 :valid { background: green; }
#form-2 :invalid { background: red; }
</style>
Per the spec, :valid and :invalid apply to form nodes as well. Only implemented in Firefox.
<form id="form-a">
<input type="text" required value="Lorem Ipsum">
<input type="text" required>
</form>
<style>
#form-a:valid { background: green; }
#form-a:invalid { background: red; }
</style>
:invalid {
box-shadow: none; /* FF */
outline: 0; /* IE */
}
:valid & :invalid apply immediately, before user interaction.
Only match after interaction or attempted submission (full algorithm).
<form id="form-3">
<input type="text" required value="Lorem Ipsum">
<input type="text" required>
<button type="submit">Go</button>
</form>
<style>
#form-3 :-moz-ui-valid {
background: green; color: white;
}
#form-3 :-moz-ui-invalid {
background: red; color: white;
}
</style>
Only use :valid and :invalid after interaction
DemoYou can't.
WebKit used to have these pseudo-elements.
Chrome 28 removed the pseudo-elements for styling form validation messages.
<form id="form-b">
<input required>
<button>Go</button>
</form>
<script>
document.getElementById( "form-b" )
.addEventListener( "invalid", function( event ) {
event.preventDefault();
}, true );
</script>
Fallback to server side validation
curl --data "param1=value1" http://foo.com/bar.do
Polyfill