- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - pattern Attribute
The HTML pattern attribute is usually used with input elements, particularly text fields like input and textarea. It is used to specify a regular expression pattern that the user's input must match in order for the form submission to be accepted.
This attribute is frequently combined with the title attribute to offer a user-friendly tip or error message that clarifies the intended input type. The pattern attribute is an effective tool for validating and managing user input, assuring data accuracy and consistency in web forms.
Syntax
Following is the syntax of the pattern attribute −
<input pattern="regexp">
Example
Considering the following example, where we are going to apply the pattern attribute for the character validation.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'pattern' attribute</title> </head> <body> <!--example of the HTML pattern attribute--> <p>HTML 'pattern' attribute</p> <form> Name(only character): <br> <input type="text" pattern="[ A-Za-z]{8}" placeholder="Enter name"> <button>Submit</button> </form> </body> </html>
When we run the above code, it will generate an output consisting of the input field along with a click button on the webpage. when the user tries to fill the input field irrespective of the characters it will throw exception.
Example
Let's look into the another scenario, where we are going to apply the pattern attribute for the number validation
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'pattern' attribute</title> </head> <body> <!--example of the HTML pattern attribute--> <p>HTML 'pattern' attribute</p> <form> Pin code(only numbers): <br> <input type="password" pattern="[0-9]{6}" placeholder="Enter pin code"> <button>Submit</button> </form> </body> </html>
On running the above code, the output window will pop up displaying the input field along with a click button on the webpage. when the user tries to fill the input field irrespective of the characters it will throw exception.
Example
Let's look at the following example, where we are going to use the pattern attribute with the input elements to specifies the regular expression to match the entered value.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'pattern' attribute</title> <style> form { width: 300px; height: 220px; background-color: rgb(6, 121, 121); border-radius: 10px; } form h2 { text-align: center; font-family: sans-serif; position: relative; top: 5px; color: white; } form label { margin: 10px 10px; } form input { padding: 8px; margin: 2px; color: white; } form button { width: 100px; margin: 10px; padding: 7px; cursor: pointer; } input:valid { background-color: green; } input:invalid { background-color: rgb(243, 86, 86); } </style> </head> <body> <!--example of the HTML pattern attribute--> <p>HTML 'pattern' attribute</p> <form> <h2>Login page</h2> <label>Username</label> <input type="text" name='uname' pattern="\w[ a-zA-Z]{5,14}" required> <br> <label>Password</label> <input type="text" name='password' pattern="\d{8,8}" required> <br> <button>Submit</button> </form> </body> </html>
When we run the above code, it will generate an output consisting of the input field along with a click button on the webpage.if the user tries to fill the input field that doesn't match the regular expression it will throw a exception.
To Continue Learning Please Login
Login with Google