HTML <input> required Attribute

HTMLWeb DevelopmentFront End Technology

The required attribute of the <input> element is used to set a field which is required to be filled before the form is submitted. If the field set with required attribute is not filled, then on clicking the SUBMIT button, the form won’t submit.

Following is the syntax −

<input required>

Let us now see an example to implement the required attribute of the <input> element. Here, we have set 3 fields as required −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Register</h2>
<form action="" method="get">
   Id − <input type="text" name="id" placeholder="Enter UserId here..." required><br>
   Password − <input type="password" name="pwd" placeholder="Enter password here..." required><br>
   DOB − <input type="date" name="dob" placeholder="Enter date of birth here..."><br>
   Telephone − <input type="tel" name="tel" placeholder="Enter mobile number here..." required><br>
   Email − <input type="email" name="email" placeholder="Enter email here..."><br><br>
   <button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>

Output

Let’s say we will click the Submit button without filling any of the 3 fields ID, Password and Telephone, then the form won’t submit. We haven’t filled the field “Telephone” here, therefore an error would be visible that won’t allow us to submit the form −

raja
Published on 18-Jun-2019 08:39:09
Advertisements