Last Updated : 19 Sep, 2024
Performing form validation for a required field in HTML ensures that a user must fill in specific fields before submitting a form. This is typically done using the required attribute, which prevents form submission if the field is left blank, ensuring proper data input.
Required attributeThe required attribute in HTML ensures that an input field must be filled before form submission. It applies to various input types (text, email, password, etc.), preventing the form from being submitted if the field is left empty.
Syntax<input type=". " id=" " name=" " required>
Example: In this example demonstrates the use of the required attribute, ensuring that both the "Name" and "Email" fields must be filled before form submission, preventing empty submissions and enforcing user input validation.
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:
Maxlength attributeThe maxlength attribute in HTML limits the number of characters a user can input in a field. It helps prevent excessive input by restricting the maximum characters, ensuring proper data length in fields like text, password, or number.
Syntax<input type="..." id="..." name="..." maxlength="...">
Note: When the maxlength attribute is set, it will not allow the user to enter more characters than those specified.
Example: In this example showcases the maxlength attribute, limiting the Roll No. input field to 10 characters. It ensures users cannot exceed the specified character limit, enhancing data input control and validation.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML maxlength attribute</title>
<style>
button {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>HTML maxlength attribute</h1>
<form>
<label>Roll No.</label>
<input type="text" maxlength="10" required />
<label>Email:</label>
<input type="email" required />
<button>Submit</button>
</form>
</body>
</html>
Output:
Minlength attributeThe minlength attribute in HTML sets the minimum number of characters required for an input field. It ensures users provide adequate data, commonly used in fields like passwords or phone numbers for validation.
Syntax<input type=" " id=" " name=" " minlength=" ">
Note: When the minlength attribute is set, an input field will not accept lesser characters than those specified.
Example: In this demonstrates the minlength attribute, requiring the Roll No. input field to have at least 5 characters. It ensures users provide sufficient data before submitting the form for validation.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML minlength attribute</title>
<style>
button {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>HTML minlength attribute</h1>
<form>
<label>Roll No.</label>
<input type="text" minlength="5" required />
<label>Email:</label>
<input type="email" required />
<button>Submit</button>
</form>
</body>
</html>
Output:
Min and max attributesThe min and max attributes in HTML set the minimum and maximum allowable values for input fields, such as numbers, dates, or ranges, ensuring users input values within a defined range for validation.
Syntax<input type=" " id=" " name=" " max=" "> <input type=" " id=" " name=" " min=" " max=" ">
Example: In this example we demonstrates the min and max attributes for the Amount field, limiting values between 50 and 100, while also including a required "Email" field for submission.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML min and max attribute</title>
<style>
button {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>>HTML min and max attribute</h1>
<form>
<label>Amount</label>
<input type="number" min="50" max="100" required />
<label>Email:</label>
<input type="email" required />
<button>Submit</button>
</form>
</body>
</html>
Output:
Multiple attributeThe multiple attribute can help us to allow a user to enter more than one value in a single input field. The multiple attribute can be used only for email and file input types.
Syntax:<input type=" " id=" " name=" " multiple>
Example: In this example we demonstrates the min and max attributes for the Amount field, restricting values between 50 and 100, and the multiple attribute for allowing multiple email addresses in the "Email" field.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML multiple attribute</title>
<style>
button {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>HTML multiple Attribute</h1>
<form>
<label>Amount</label>
<input type="number" min="50" max="100" required />
<label>Email:</label>
<input type="email" multiple required />
<button>Submit</button>
</form>
</body>
</html>
Output:
Pattern attributeThe pattern attribute in HTML specifies a regular expression that an input field's value must match. It ensures users provide data in a specific format, enhancing validation for fields like email, phone numbers, or passwords.
Syntax:<input type=" " id=" " name=" " pattern=" ">
Example: In this example we use the pattern attribute to ensure the "Tel." field matches a 10-digit number, and the multiple attribute allows multiple email addresses in the Email field.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML pattern attribute</title>
<style>
button {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>HTML pattern attribute</h1>
<form>
<label>Tel.</label>
<input type="text" pattern="[0-9]{10}" required />
<label>Email:</label>
<input type="email" multiple required />
<button>Submit</button>
</form>
</body>
</html>
Output:
In the above example, the pattern specifies that Tel. can be composed of digits only and its length must be 10.This is how we can make the form data required or add certain checks on the input that is entered.
How to perform form validation for a required field in HTML ?
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