Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use a novalidate attribute in HTML?
The novalidate attribute in HTML is a Boolean attribute that disables the browser's built-in form validation when a form is submitted. When applied to a <form> element, it allows users to submit forms even if the input fields contain invalid data according to their validation constraints.
By default, HTML5 provides automatic client-side validation for various input types such as email, number, url, and attributes like required, pattern, min, and max. The novalidate attribute overrides this behavior.
Syntax
Following is the syntax for using the novalidate attribute −
<form novalidate> <!-- form content --> </form>
Since it is a Boolean attribute, you can also write it as novalidate="novalidate" or simply novalidate. Both formats are valid in HTML5.
Form Without novalidate Attribute
First, let's see how a form behaves with default HTML5 validation enabled −
Example
<!DOCTYPE html>
<html>
<head>
<title>Form with Default Validation</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Student Registration (With Validation)</h2>
<form action="/submit" method="post">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="age">Age:</label><br>
<input type="number" id="age" name="age" min="18" max="65" required><br><br>
<input type="submit" value="Submit">
</form>
<p><em>Try submitting with invalid data - the browser will show validation errors.</em></p>
</body>
</html>
In this form, the browser will validate the email format and ensure the age is between 18 and 65 before allowing submission. If validation fails, error messages appear automatically.
Form With novalidate Attribute
Now let's see the same form with the novalidate attribute applied −
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML novalidate Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Student Registration (No Validation)</h2>
<form action="/submit" method="post" novalidate>
<label for="student-name">Student Name:</label><br>
<input type="text" id="student-name" name="sname" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="rank">Rank:</label><br>
<input type="number" id="rank" name="rank" min="1" max="100"><br><br>
<input type="submit" value="Submit">
</form>
<p><em>This form will submit even with invalid email format or empty required fields.</em></p>
</body>
</html>
With the novalidate attribute, this form will submit regardless of whether the email is properly formatted, required fields are empty, or the rank is outside the specified range (1-100).
Dynamic Validation Control with JavaScript
You can toggle the novalidate attribute dynamically using JavaScript to enable or disable validation as needed −
Example
<!DOCTYPE html>
<html>
<head>
<title>Toggle Form Validation</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Dynamic Validation Control</h2>
<form id="myForm" action="/submit" method="post">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone:</label><br>
<input type="tel" id="phone" name="phone" pattern="[0-9]{10}" required><br><br>
<input type="submit" value="Submit">
</form>
<button onclick="toggleValidation()">Toggle Validation</button>
<p id="status">Current Status: Validation Enabled</p>
<script>
function toggleValidation() {
var form = document.getElementById("myForm");
var status = document.getElementById("status");
if (form.hasAttribute("novalidate")) {
form.removeAttribute("novalidate");
status.textContent = "Current Status: Validation Enabled";
} else {
form.setAttribute("novalidate", "");
status.textContent = "Current Status: Validation Disabled";
}
}
</script>
</body>
</html>
Click the "Toggle Validation" button to switch between enabled and disabled validation modes. When validation is disabled, the form accepts invalid email formats and incomplete phone numbers.
Common Use Cases
The novalidate attribute is typically used in the following scenarios −
-
Custom validation − When implementing custom JavaScript validation logic instead of relying on browser defaults.
-
Draft saving − Allowing users to save incomplete forms as drafts without triggering validation errors.
-
Testing − During development and testing phases to bypass validation temporarily.
-
Legacy compatibility − When maintaining older forms that relied on server-side validation only.
Browser Compatibility
The novalidate attribute is supported in HTML5 and works across all modern browsers including Chrome, Firefox, Safari, and Edge. It has no effect in older browsers that don't support HTML5 form validation.
Conclusion
The novalidate attribute provides a simple way to disable HTML5's automatic form validation. While useful for custom validation scenarios and testing, remember that disabling client-side validation means you must implement proper server-side validation to ensure data integrity and security.
