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:
JavaScript
x
7
1
var isValid = true;
2
var id=document.getElementById("pid").value;
3
if (!id.match(/[A-Za-z]{2}+[0-9]+[A-Za-z]{1}/)) {
4
document.getElementById("pidMessage").innerHTML="Your pid format is invalid!";
5
isValid = false;
6
}
7
Advertisement
Answer
You need to add the begging ^
and end $
signs
JavaScript
1
7
1
let isValid = true;
2
let id=document.getElementById("pid").value;
3
if (!id.match(/^([A-Za-z]{2}[0-9]+[A-Za-z]{1})$/)) {
4
document.getElementById("pidMessage").innerHTML="Your pid format is invalid!";
5
isValid = false;
6
}
7
And for everyone’s sake use let
not var