简体   繁体   中英

Check for 4 numbers and 2 letters if the first 4 characters are numbers

I have some trouble with checking for certain values and check if the value is correct.

Now I have made it work with always 4 numbers and 2 letters:

$("input[id='post']").keyup(function count() {
    var input = this.value;
    var regex = new RegExp(/^[0-9]{4}[a-z]{2}$/i);
    console.log(regex.test(input));
});

This works great.

Sometimes though, my input also takes words so these words do need to go through. At the moment only 4 numbers and 2 digits are allowed.

It's basically if there is a word, so no numbers at all, I need to let that word go through.

change your regex as below and try like below

$("input[id='post']").keyup(function count() {
    var input = this.value;
    var regex = new RegExp(/^([0-9]{4}[a-z]{2}|[a-z]*)$/i);
    console.log(regex.test(input));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM