How to validate input field in the HTML Form ?
In this article, we will see how to validate form input fields using HTML. To validate the form using HTML, we will use HTML <input> required attribute. The <input> required attribute is a Boolean attribute that is used to specify the input element must be filled out before submitting the Form.
Syntax:
<input required>
Approach: In the below example, we will create a form with <label> and <input> tags and add required attribute with input tag to validate the input field using HTML.
Example:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < title > How to validate input field in HTML Form? </ title > </ head > < body > < center > < h1 style = "color: green;" > GeeksforGeeks </ h1 > < h3 > How to validate input field in HTML Form? </ h3 > < form action = "#" > < label for = "fname" >First Name:</ label > < input type = "text" name = "fname" id = "fname" required> < br >< br > < label for = "lname" >Last Name:</ label > < input type = "text" name = "lname" id = "lname" required> < br >< br > < label for = "email" >Email Id:</ label > < input type = "email" name = "email" id = "email" required> < br >< br > < input type = "submit" value = "Submit" > </ form > </ center > </ body > </ html > |
Output: