Required attribute
If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.
Syntax:
<input type=". " id=" " name=" " required>
Example: The implementation of required attribute
HTML
<!DOCTYPE html> < html > < head > < title >HTML required attribute</ title > < style > button { margin-top: 5px; } </ style > </ head > < body > < h1 >HTML 'required' Attribute</ h1 > < form > < label >Name:</ label > < input type = "text" required /> < label >Email:</ label > < input type = "email" required /> < button >Submit</ button > </ form > </ body > </ html > |
Output:
How to perform form validation for a required field in HTML ?
The form data is very important for a website and the correct form data or valid form data must be submitted to the form. To make sure that the form data is valid or to make an input field mandatory, various attributes can be used. We shall discuss the following attributes in this article & will understand them with the help of examples.
Table of Content
- Required attribute
- Maxlength attribute
- Minlength attribute
- Min and max attribute
- Multiple attribute
- Pattern attribute