Using required Attribute in HTML5
The required attributes are
used with the input field when input entry is required. When we use these attributes
with input type, then we can not keep it blank because the browser will not allow
it. Now we use the required attribute in form. We write the following code:
- <!DOCTYPE HTML>
- <html>
- <body>
- <form method="post">
- First Name
- <input type="text" name="fname" required="required" />
- Last Name
- <input type="text" name="lname" />
- </br>
- <input type="submit" />
- </form>
- </body>undefinedundefined</html>
Then we run this code. The output will be as like below figure:
If we keep fname input field blank and click on submit button then message
is displayed as like below figure: