I want to limit the number of letters at the beginning and end of the input value (e.g. QQ23F and SG21G) through JavaScript but I found that only one {} can be written in a pattern. Thanks for any help. Here is my incorrect code:
var isValid = true; var id=document.getElementById("pid").value; if (!id.match(/[A-Za-z]{2}+[0-9]+[A-Za-z]{1}/)) { document.getElementById("pidMessage").innerHTML="Your pid format is invalid!"; isValid = false; }
Advertisement
Answer
You need to add the begging ^
and end $
signs
let isValid = true; let id=document.getElementById("pid").value; if (!id.match(/^([A-Za-z]{2}[0-9]+[A-Za-z]{1})$/)) { document.getElementById("pidMessage").innerHTML="Your pid format is invalid!"; isValid = false; }
And for everyone’s sake use let
not var