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 −


 Live Demo

<!DOCTYPE html>
<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>


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 −

Published on 18-Jun-2019 12:09:09