Kuubs
Kuubs

Reputation: 1340

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.

Upvotes: 1

Views: 462

Answers (1)

Shankar
Shankar

Reputation: 3088

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));
});

Upvotes: 2

Related Questions