Skip to content

03. Form Validation

Form validation ensures that users enter correct and complete data before submission. HTML5 provides built-in validation through attributes like required, pattern, min, max, and input types. Understanding form validation helps create better user experiences and prevents invalid data submission.

Makes field mandatory:

<input type="text" name="name" required>

Input type validation:

<input type="email" name="email">
<input type="url" name="website">
<input type="number" name="age">

Custom validation pattern:

<input type="text" name="phone" pattern="[0-9]{10}">

Numeric limits:

<input type="number" name="age" min="18" max="100">

Text length limits:

<input type="text" name="username" minlength="3" maxlength="20">

Disable validation:

<form novalidate>
<!-- Form without validation -->
</form>

Disable validation for specific button:

<button type="submit" formnovalidate>Submit without validation</button>
<input type="email" name="email" required>
<input type="tel" name="phone" pattern="[0-9]{10}">
<input type="email" name="email" required
oninvalid="this.setCustomValidity('Please enter a valid email')">