1

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!

enter image description here

5
  • Show JS code how and when you validate it. Commented Sep 5, 2018 at 9:18
  • Please provide a minimal reproducible example Commented Sep 5, 2018 at 9:27
  • Try my updated answer and its fiddles :) Commented Sep 5, 2018 at 9:46
  • In what way does it fail when you use pattern? Commented Sep 5, 2018 at 10:08
  • I'll post my code Commented Sep 6, 2018 at 1:50

3 Answers 3

2

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

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

Try this online


And if you want to use javascript:

HTML:

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

javascript:

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

Try it online


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

HTML:

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

javascript:

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 this.style.backgroundColor="#f88";
       this.style.backgroundColor="#0f0";
    });
<input type="text" name="formField" placeholder='Type here 4 test!'/>

Sign up to request clarification or add additional context in comments.

2 Comments

works fine if i copy paste a valid format, but manually typing still returning false.
Have you tested my fiddles? all of them works correctly. Which part of my code do you use? is possible to put simple code (as fiddle in a comment) that you are using it and has this problem? I've added another part to my response for check manually typing.
1

Same pattern code will work in JS

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

Comments

1

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';
console.log(/^[A-Z]{4}[0-9]{7}$/.test(container));
container='QWSA123121';
console.log(/^[A-Z]{4}[0-9]{7}$/.test(container));
container='QWSA12312312';
console.log(/^[A-Z]{4}[0-9]{7}$/.test(container));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.