Learning object 44 of 100
Input validation laboratory
Change a field's constraints and value and see every validity flag, checkValidity, custom messages and the :invalid styling update.
Loading…
What it shows
HTML attributes such as required, type, min, max, minlength and pattern describe rules the browser checks. field.validity reports which rule failed with flags such as valueMissing, typeMismatch, rangeOverflow and patternMismatch, and checkValidity returns the overall result. setCustomValidity adds your own rules and must be cleared with an empty string once the value is fine.
Guided experiment
- In Validity flags, type values into the field in the preview and press Check.
- In Custom rule, type a password that is long enough but has no digit.
- In Form submission, press Join with empty fields. Which events fire?
Predict
A field has min="1" and the value "0". Which flag is true?
Related
Lessons: Lesson 63: Native browser validation, Lesson 64: Custom validation rules
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-044.js in the companion repository.