HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

Understanding the Regex Attribute in HTML <input> Tags

The pattern attribute in HTML <input> tags uses regular expressions (regex) to define constraints on what users can enter into form fields. This feature is essential for validating user input and ensuring data is in the correct format before being processed or submitted.

What is the pattern Attribute?

The pattern attribute is used with <input> elements to specify a regex that the <input> element's value must match in order to be considered valid. It is only applicable to certain types of input, such as text, date, search, url, tel, email, and password.

Basic Usage of the pattern Attribute

Here is how the pattern attribute can be used to restrict input:

<form action="/submit-form" method="post">
    <label for="phone">Enter your phone number (Format: 123-456-7890):</label>
    <input type="tel" id="phone" name="phone" 
         pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" 
         title="Enter a phone number in the format: 123-456-7890" required>
    <input type="submit">
</form>

In this example, the pattern attribute is used to ensure that the telephone number entered by the user matches the format 123-456-7890. If the user tries to submit the form with the telephone number in a different format, the form will not validate, and the user will be prompted to enter the number in the correct format.

Complex Regex Examples

Regex can be used to create more complex patterns for validation. Here are a few examples:

Why Use the pattern Attribute?

Using the pattern attribute improves the user experience by catching input errors before the form is submitted. This leads to fewer errors in data processing and better user interactions as feedback is immediate and specific. Additionally, it reduces the need for extensive JavaScript validation, simplifying your code.

Conclusion

The pattern attribute in HTML <input> tags is a powerful tool for performing client-side validation of form inputs using regex. It helps ensure that data submitted by the user adheres to a specified format, enhancing data integrity and user experience.