HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML pattern Attribute
Photo Credit to CodeToFun
🙋 Introduction
The pattern
attribute is a powerful feature in HTML that allows developers to define a regular expression pattern for validating the input in a form field.
This attribute is commonly used with text-based input fields, such as textboxes and password fields, to ensure that the entered data matches a specified pattern.
🎯 Purpose of pattern
The primary purpose of the pattern
attribute is to enforce a specific format for user input.
By providing a regular expression pattern, developers can guide users in entering data that adheres to a predefined structure.
This is particularly useful for collecting consistent and valid information through web forms.
💎 Values
The pattern
attribute accepts a regular expression pattern as its value. Regular expressions are powerful tools for matching patterns in strings. When applied to the pattern
attribute, the regular expression defines the valid format for the input.
For example, a pattern for a valid email address might look like this:
<input type="email" pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}">
🧠 How it Works
In this example, the pattern
attribute ensures that the input follows the standard format of an email address.
📄 Example
Let's look at a simple example of how to use the pattern
attribute in an HTML form:
<form>
<label for="phone">Phone Number:</label>
<input type="text" id="phone" name="phone" pattern="\d{3}-\d{3}-\d{4}" placeholder="XXX-XXX-XXXX" required>
<small>Enter the phone number in the format XXX-XXX-XXXX.</small>
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the pattern
attribute is used to enforce a specific pattern for the phone number input, requiring users to enter it in the format XXX-XXX-XXXX.
🔄 Dynamic Values with JavaScript
The pattern
attribute can also be manipulated dynamically with JavaScript. This allows you to change the validation pattern based on user interactions or other dynamic factors.
<script>
// Dynamically set a new pattern for an input field
document.getElementById("dynamicPatternField").pattern = "[A-Za-z]{3}";
</script>
🧠 How it Works
In this script, the pattern
attribute is dynamically set to "[A-Za-z]{3}" for an input field with the id "dynamicPatternField." This can be useful when you need to adjust validation patterns based on changing requirements.
🏆 Best Practices
- Choose or create regular expression patterns that accurately match the expected format for the input field.
- Provide clear instructions to users on the expected input format, especially when using patterns that might not be immediately obvious.
- Always perform server-side validation to ensure data integrity and security, as client-side validation can be bypassed.
🎉 Conclusion
The pattern
attribute is a valuable tool for enforcing specific input formats in HTML forms.
By utilizing regular expressions, developers can guide users in providing accurate and valid data, contributing to a more seamless and error-free user experience.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML pattern Attribute), please comment here. I will help you immediately.