How to validate that there is not a minimum of characters but a maximum?

2

I am preparing a regular expression:

[a-zA-Z0-9._-]{20}

So far I am with the previous part of the "@" of an email and I am trying to do something like this:

[a-zA-Z0-9._-]{"*"-20}

Where "*" appears, I try to put it to not have a minimum size, but a maximum

How could you mount the expression?

The idea is to use it later to do this:
E-mail in various characters (letters, numbers, periods or dashes, high or low, 20 maximum) followed by arroba, letters (20 maximum), period (1), and 2 or 3 letters.

    
asked by Eduardo 29.11.2017 в 19:30
source

1 answer

7

The solution is to use {n,m} ( cuantificador de rango ), where n is the minimum number of possible occurrences and m the maximum.

Example:

var re = /^[a-zA-Z0-9]{0,20}$/;

console.log(re.test('')); // 0 chars
console.log(re.test('1')); // 1 chars
console.log(re.test('12345678901234567890'));// 20 chars
console.log(re.test('123456789012345678901'));// 21 chars
    
answered by 29.11.2017 / 20:12
source