Last Updated : 06 Jan, 2025
Input fields are essential components of HTML forms, enabling users to provide data for processing. Ensuring proper validation of these input fields enhances user experience and prevents errors or malicious inputs.
What We Are Going to Create?We will create simple Validating Input Fields in HTML Forms.
<html>
<head></head>
<body>
<center>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
Validate Input Fields in HTML Form
</h3>
<form action="#" method="post">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname"
placeholder="Enter your first name" required minlength="2">
<br><br>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname"
placeholder="Enter your last name" required minlength="2">
<br><br>
<label for="email">Email Id:</label>
<input type="email" name="email" id="email"
placeholder="Enter your email address" required>
<br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password"
placeholder="Enter a secure password" required
minlength="8">
<br><br>
<label for="phone">Phone Number:</label>
<input type="tel" name="phone" id="phone"
placeholder="XXX-XXX-XXX" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Format: 123-456-7890" required>
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
type="email"
.123-456-7890
using a regex pattern.Submit
button is included to send the form data upon successful validation.required
, minlength
, and pattern
for real-time browser validation, ensuring user inputs are correct.title
) to explain input formats, making the form intuitive and user-friendly.Atributes
Uses
It ensures the user fills out a field before the form is submitted.
Sets a maximum number of characters that can be entered.
Sets a minimum number of characters required.
Pattern
Specifies a regular expression that the input value must match
Type
Determines the type of input and enforces specific validation rules.
Min
and Max
Set minimum and maximum values for numeric or date inputs.
Provides a tooltip with guidance on input requirements when the input does not match the pattern
Multiple
Allows multiple entries for certain types of inputs like email or file.
Displays a hint or example text inside the input field.
Disabled
Disables the input, making it uneditable and excluding it from form submission.
Controls whether the browser should automatically fill in values based on previous inputs.
Automatically focuses on a specific input field when the page loads.
Makes the input field uneditable but includes it in the form submission
Disables native HTML form validation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4