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!'/>
pattern?