Javascript - How to create regex similar to html pattern attribute

html pattern not working
html input pattern numbers only
html pattern phone number
html pattern special characters
html pattern generator
html pattern for name with space
email pattern in html
html5 pattern for alphabets only

I'm using pattern attribute of html but it's failing in for some reason so I'm thinking on how to create regex for javascript.

Highlighted part of the image is the format that I'm validating using this regular expression pattern attribute pattern="[A-Z]{4}[0-9]{7}". I still can't manage to build this using javascript.

Any answers/reference/solution is appreciated!

Same pattern code will work in JS

var pattern = /[A-Z]{4}[0-9]{7}/
var qry = 'ASDF4567543';
if(qry.match(pattern)) {

HTML input pattern Attribute, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Can you post your html also .Are you sure you have attribute like [a-zA-Z0-9_] this in your html .i think its a pattern attr – prasanth May 30 '17 at 7:36

Your regex will work fine in JS. You just need to add start (^) and end ($) of string anchors to it, otherwise it will also match strings that are longer than 11 characters.

let container='QWSA1231231';

HTML pattern Attribute, The pattern attribute specifies a regular expression that the <input> element's value is Tip: Learn more about regular expressions in our JavaScript tutorial. Thanks for contributing an answer to Vi and Vim Stack Exchange! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.

Your RegExp is correct but use ^ in first and $ in last of it. then add required attribute like this:

   <input type="text" name="formField" pattern="^[A-Z]{4}[0-9]{7}$" required />
   <input type='submit'/>

Try this online

And if you want to use javascript:


   <input type="text" name="formField" />
   <input type="button" value='submit'/><input style='display: none' type='submit'/>


   if(/^[A-Z]{4}[0-9]{7}$/.test(document.querySelector('[name="formField"]').value)==false) return alert("Please check input value and try again!");

Try it online

And, if you have not form, you can try this simple way:


<input type="text" name="formField" />
<input type="button" value='Add'/>


document.querySelector('[type="button"]').addEventListener("click", function(){//or onclick = ...
   if(/^[A-Z]{4}[0-9]{7}$/.test(document.querySelector('[name="formField"]').value)==false) return alert("Please check input value and try again!");
   alert("Is OK!!");

Try it too

Try this for check manually typing:

document.querySelector('[name="formField"]').addEventListener("keyup", function(ev){
       if(/^[A-Z]{4}[0-9]{7}$/.test(document.querySelector('[name="formField"]').value)==false) return"#f88";"#0f0";
<input type="text" name="formField" placeholder='Type here 4 test!'/>

HTML DOM Input Text pattern Property, The pattern attribute specifies a regular expression that the text field's value is checked Tip: Learn more about Regular Expressions in our JavaScript Tutorial. When the input does not adhere to the pattern, the browser can indicate to the user about the format using the contents of the “title” attribute. Hands On. Here is a simple HTML5 listing showing how the “pattern” attribute works to enforce input in the desired format expressed as a regular expression. <!DOCTYPE html > < html > < head

HTML attribute: pattern, The pattern attribute specifies a regular expression the form control's different from JavaScript regular expressions: in the case of pattern  This post is part of a series called Strange and Unusual HTML. In this tutorial we’ll explore HTML’s pattern attribute, using it to help us customize the way we validate our forms. Form validation is of vital importance to a website’s security as well as its usability. The validation process evaluates whether the input value is in the

Input Pattern: Use It To Add Basic Data Validation In HTML5 », of the text, tel, email, url, password, and search input types. It must be a valid JavaScript regular expression, as used by the RegExp type, and as documented in our guide on regular expressions; the 'u' flag is specified when compiling the regular expression, so that the pattern is treated as a sequence of Unicode code points, instead of as ASCII. No forward slashes should be specified around the pattern text.

How to limit the number of characters allowed in form input text field?, In this video we take a look at the new "pattern" attribute in HTML which If this video Duration: 4:59 Posted: Dec 31, 2018 Data Validation with Regular Expressions. The pattern attribute of the <input> element allows you to add basic data validation without resorting to JavaScript. It works by matching the input value against a regular expression. A regular expression is a formalized string of characters that define a pattern.