When creating a form, it’s important not only to place the fields correctly, but also to ensure that the user enters valid data. HTML provides many built-in validation tools — without JavaScript.
In this lesson, we will explore the main ways to validate an HTML form.
requiredPrevents form submission when the field is empty.
type="email"Checks whether the text looks like an email.
type="number", min, maxminlength, maxlengthtype="url"type="date" with min / maxnovalidate, formnovalidateDisable validation for the entire form:
Disable validation for one button:
pattern attributeAllows custom validation rules using regular expressions.
Regular Expression Table for pattern
| Purpose | Expression | Example | Description |
|---|---|---|---|
| Digits only | [0-9]+ |
12345 | Any amount of digits |
| Exactly 10 digits | \d{10} |
0671234567 | Phone without country code |
| International phone | \+\d{11,14} |
+380671234567 | Plus + 11–14 digits |
| English letters only | [A-Za-z]+ |
Hello | No spaces |
| Letters (any language) | \p{L}+ |
Привіт | Depends on browser support |
| Letters + spaces | [A-Za-z ]+ |
John Doe | Spaces allowed |
| Email (simple) | [a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4} |
test@mail.com | Basic email check |
| Password: letters + digits | (?=.*[0-9])(?=.*[A-Za-z]).{6,} |
Abc123 | At least 6 characters |
| Cyrillic only | [А-Яа-яЁёЇїІіЄєҐґ]+ |
Пример | Only Cyrillic symbols |
| Car plate (UA) | [A-Z]{2}\d{4}[A-Z]{2} |
AA1234BB | Ukrainian plate format |
Summary
We covered:
required
validation via type="email", type="url", type="number"
min, max, minlength, maxlength
disabling validation
custom validation using pattern
Knowledge Check: HTML Basics Test
63
HTML from Scratch: Adding Audio and Video to Your Website — For Beginners. Lesson #26
82
HTML from Scratch: The download Attribute in HTML5 (for downloading files). Lesson #25
149
HTML from Scratch: Commenting in HTML. Lesson #24
507
HTML from Scratch: The DOCTYPE Tag. Lesson #23
124
Leave a Reply