How to specify that the form should not be validated when submitted in HTML?


The <form> element is an essential component available in HTML that enables us to create interactive sections on websites like Contact forms, Login forms, etc. Through the form element, users can input and submit data to web servers. The <form> element is versatile and may include a number of different input fields such as text boxes, radio buttons, checkboxes, drop-down menus, and buttons. Also, the <form> element consists of multiple attributes such as "action" and "method" that are used in determining the destination of the data submitted and the method of processing.

Form validation is one of the key parts in <form> element which is used to check the data entered by users into the form to ensure that it conforms to the necessary requirements. The validation allows us to check whether the submitted data is accurate, complete, and in the correct format. The form validation of checking the data can be done using two primary approaches- client-side and server-side.

In this article, we’ll see how to specify that the form should not be validated when submitted in HTML. We’ll discuss three different approaches to performing this task in HTML.

Different Approaches to Specifying Form Not Validated

There are three different approaches to checking whether the form is valid or not. Let’s discuss all of these approaches one by one in detail.

Approach 1: Using the ‘novalidate’ Form Attribute

The first approach is to use the "novalidate" attribute in the form tag. This attribute disables the browser's default validation behavior, which means that when the form is submitted, it will not be validated.

Syntax

<form action="/data-submitted" method="post" novalidate>
   <!-- your other input fields go here -->
</form>

In the above code, we have added the "novalidate" attribute to the form tag. This tells the browser not to validate the form when it is submitted. Note that this approach only disables the browser's default validation behavior. If you have custom validation logic, it will still be executed.

Example

In this example, we have an HTML file with a form that includes the novalidate attribute on the <form> element. This attribute tells the browser to disable validation for the entire form. When the user clicks the submit button, the form will be submitted without any validation being performed.

<html>
<head>
	<title>Using novalidate attribute</title>
</head>
<body>
	<form method="post" action="data-submit.php" novalidate>
		<label for="username">Username:</label>
		<input type="text" id="username" name="username" required><br>
		<label for="email">Email:</label>
		<input type="email" id="email" name="email" required></br>		
        <label for="password">Password:</label>
		<input type="password" id="password" name="password" required><br>
	   <button type="post">Add</button>
	</form>
</body>
</html>

Approach 2: Using the ‘formnovalidate’ Form Attribute

The second approach is to use the "formnovalidate" attribute in the submit button. This attribute tells the browser not to validate the form when the button is clicked.

The formnovalidate attribute overrides the novalidate attribute of the <form> element and this formnovalidate attribute can be used with type="submit" available in <button> element.

Syntax

<form action="/data-submitted" method="post" novalidate>
   <!-- your other input fields go here →
   <button type="add" formnovalidate>Add</button>
</form>

In the above example, we have added the "formnovalidate" attribute to the submit button. This tells the browser not to validate the form when the button is clicked. Note that this approach only disables the browser's default validation behavior. If you have custom validation logic, it will still be executed.

Example

In this example, we have an HTML file with a form that contains two submit buttons. The first submit button includes the formnovalidate attribute, which tells the browser to submit the form without performing validation. The second submit button does not include this attribute and will trigger the browser's default validation behavior.

<html>
<head>
   <title>Using formnovalidate attribute</title>
</head>
<body>
   <form method="post" action="data-submit.php">
      <label for="username">Username:</label>
      <input type="text" id="username" name="username" required><br/>
      <label for="email">Email:</label>
      <input type="email" id="email" name="email" required><br/>
      <label for="password">Password:</label>
      <input type="password" id="password" name="password" required> <br/>
      <button type="post" formnovalidate>Add</button>
   </form>
</body>
</html>

Approach 3: Using Custom JavaScript to Prevent Form Validation

The third approach is to use JavaScript to prevent the default form validation behavior when the form is submitted. This approach gives the more control over the validation behavior and allows us to customize the validation logic.

Syntax

<form action="/data-submitted" method="post" novalidate>
   <!-- your other input fields go here →
   <button type="add" formnovalidate>Add</button>
</form>
<script>
   const formValidate = document.getElementById(data-form');
   formValidate.addEventListener(add, function(event) {
      event.preventDefault();
      //other data goes here
   });
</script>

In the above code, we have added an event listener to the form submission event using JavaScript. This event listener prevents the default behavior of the form when it is submitted. You can then add your custom validation logic and submit the form data programmatically.

Example

In this example, we have a form that includes a JavaScript function called validateDataForm() that retrieves the values entered by the user and checks whether they meet the required criteria. If any of the validation tests fail, an error message is displayed using the alert() function, and the function returns false, preventing the form from being submitted. If all validation tests pass, the function returns true, allowing the form to be submitted.

<html>
<head>
   <title>Using formnovalidate attribute</title>
</head>
<body>
   <script>
      function validateDataForm() {
         const usernameValidate = document.getElementById("username");
			const emailValidate = document.getElementById("email");
			const passwordValidate = document.getElementById("password");
			const emailValidateRegex = /^\S+@\S+\.\S+$/;
			if (usernameValidate.value === "") {
            alert("Enter a valid username");
				return false;
			}
			if (!emailValidateRegex.test(emailValidate.value)) {
				alert("Enter a valid email");
				return false;
			}
			if (passwordValidate.value.length < 10) {
				alert("Password must be at least 10 characters long.");
				return false;
         }
			return true;
		}
	</script>
   <form method="post" action="data-submit.php">
      <label for="username">Username:</label>
		<input type="text" id="username" name="username" required><br />
      <label for="email">Email:</label>
		<input type="email" id="email" name="email" required> <br /> <label for="password">Password:</label>
		<input type="password" id="password" name="password" required> <br />
      <button type="post" onclick="return validateDataForm()">Add</button> 
	</form>
</body>
</html>

Conclusion

In this article, we have discussed how to specify that the form should not be validated when submitted in HTML and we have seen different methods to specify that a form should not be validated when submitted. We have discussed the methods including the "novalidate" attribute, the "formnovalidate" attribute, and using JavaScript that help prevent the default form validation behavior.

Updated on: 02-May-2023

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements