A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/html/how-to-validate-input-field-in-the-html-form/ below:

How To Validate Input Field In The HTML Form?

How To Validate Input Field In The HTML Form?

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.

Project Preview Validate Input Field In The HTML Form Validating Input Fields HTML HTML
<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>
HTML form validation attributes

Atributes

Uses

Required

It ensures the user fills out a field before the form is submitted.

Maxlength

Sets a maximum number of characters that can be entered.

Minlength

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.

Title

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.

Placeholder

Displays a hint or example text inside the input field.

Disabled

Disables the input, making it uneditable and excluding it from form submission.

Autocomplete

Controls whether the browser should automatically fill in values based on previous inputs.

Autofocus

Automatically focuses on a specific input field when the page loads.

Readonly

Makes the input field uneditable but includes it in the form submission

Novalidate

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