How To Validate Input Field In The HTML Form? Last Updated : 01 Dec, 2025 Comments Improve Suggest changes 6 Likes Like Report 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. Project OverviewWe will create simple Validating Input Fields in HTML Forms.A simple HTML form with various input fields to collect user data.First Name and Last Name: Text input fields with required validation and minimum character length.Email Address: Input field for email with built-in validation for proper email format.Password: Secure input field with a minimum character limit.Phone Number: Input field with a specific pattern for phone number format validationA simple "Submit" button to send the data once the form is filled and validated.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 Structure: The form uses a centered layout with headings to display "GeeksforGeeks" and a subtitle. It includes a form with labeled input fields for user data collection.First Name, Last Name: Text fields with required and minimum length validation.Email: Validates the email format using type="email".Password: Requires a minimum of 8 characters.Phone Number: Validates format 123-456-7890 using a regex pattern.Submit Button: A single Submit button is included to send the form data upon successful validation.Built-in Validation: Uses attributes like required, minlength, and pattern for real-time browser validation, ensuring user inputs are correct.User Experience Enhancements: Includes placeholders for guidance and tooltips (title) to explain input formats, making the form intuitive and user-friendly.HTML Form Validation Attributes AttributesUsesRequiredIt ensures the user fills out a field before the form is submitted.MaxlengthSets a maximum number of characters that can be entered.MinlengthSets a minimum number of characters required.PatternSpecifies a regular expression that the input value must matchTypeDetermines the type of input and enforces specific validation rules.Min and MaxSet minimum and maximum values for numeric or date inputs.TitleProvides a tooltip with guidance on input requirements when the input does not match the patternMultipleAllows multiple entries for certain types of inputs like email or file.PlaceholderDisplays a hint or example text inside the input field.DisabledIt disables the input, making it uneditable and excluding it from form submission.AutocompleteControls whether the browser should automatically fill in values based on previous inputs.AutofocusAutomatically focuses on a specific input field when the page loads.ReadonlyMakes the input field uneditable but includes it in the form submissionNovalidateDisables native HTML form validation. Suggested Quiz Edit Quiz 5 Questions Which attribute ensures that an input field must be filled before form submission? A minlength B title C readonly D required Explanation: required prevents form submission until the user enters a value. What is the purpose of the pattern attribute in an input field? A To set the maximum allowed characters B To apply CSS styling onto the input box C To validate the input using a regular expression D To automatically complete the field using past entries Explanation: pattern ensures the input matches a specific regex format, such as phone numbers. Which input type automatically validates email formatting in HTML forms? A type="email" B type="text" C type="url" D type="search" Explanation: type="email" checks for a correctly formatted email address before submission. What does the minlength attribute validate in an input field? A The earliest date allowed in a calendar input B The smallest value for numeric entries C The minimum number of characters required D The lowest number of times input can repeat Explanation: minlength enforces a minimum character count, useful for names and passwords. What is the purpose of the title attribute in form validation? A It highlights the field automatically when clicked B It displays a tooltip with guidance when the value is invalid C It resets the form inputs to default values D It prevents editing of the field while keeping it visible Explanation: The title attribute shows a tooltip explaining format requirements when the input does not match the pattern. Quiz Completed Successfully Your Score : 2/5 Accuracy : 0% Login to View Explanation 1/5 1/5 < Previous Next > Comment V vkash8574 Follow 6 Improve V vkash8574 Follow 6 Improve Article Tags : HTML HTML-Questions HTML Form Explore HTML BasicsHTML Introduction4 min readHTML Editors4 min readHTML Basics7 min readStructure & ElementsHTML Elements4 min readHTML Attributes7 min readHTML Headings3 min readHTML Paragraphs3 min readHTML Text Formatting4 min readHTML Block and Inline Elements3 min readHTML Charsets4 min readListsHTML Lists3 min readHTML Ordered Lists5 min readHTML Unordered Lists4 min readHTML Description Lists3 min readVisuals & MediaHTML Colors11 min readHTML Links Hyperlinks2 min readHTML Images7 min readHTML Favicon4 min readHTML Video4 min readLayouts & DesignsHTML Tables9 min readHTML Iframes4 min readHTML Layout4 min readHTML File Paths3 min readProjects & Advanced TopicsHTML Forms4 min readHTML5 Semantics5 min readHTML URL Encoding4 min readHTML Responsive Web Design11 min readTop 10 Projects For Beginners To Practice HTML and CSS Skills8 min readTutorial ReferencesHTML Tags - A to Z List5 min readHTML Attributes Complete Reference8 min readHTML Global Attributes5 min readHTML5 Complete Reference8 min readHTML5 MathML Complete Reference3 min readHTML DOM Complete Reference15+ min readHTML DOM Audio/Video Complete Reference2 min readSVG Element Complete Reference5 min readSVG Attribute Complete Reference8 min readSVG Property Complete Reference7 min readHTML Canvas Complete Reference4 min read Like