What are quantifiers? (the `+` and `{14,16}`)

I think the difference between the first:

``````minlength="3" maxlength="15" pattern="[a-zA-Z0-9]+"
``````

and the second:

``````pattern="[a-zA-Z0-9]{3,15}"
``````

is, that the first solution let the user know, that the amount of characters is incorrect.
But the second solution just tells the user, that the input didn’t match the requested format. So, the first solution gives the user more information on what exactly went wrong.

2 Likes

Thanks for this, I, too, was curious whether replacing the “+” with {3,15} would work the same, and realized:

• with the “{3,15}” quantifier, user can keep on typing past the 15 character maximum length, and won’t be alerted that the user has gone past the max length (of 15) until user hits “Submit” button,

• whereas with the “+” quantifier paired along with the “minlength” and “maxlength”, user is stopped from unnecessarily typing past the limit of 15 characters.

2 Likes

Ah! This is a great explanation. Thank you

Simple explanation: Repetition/Iteration
ex. try to type “bass”, it will accept the word.
Try to delete “+” from the code. since there are two S. It will not be accepted.

How about if you want to require it have at least one upper, one lower, and one number?

Far from an expert, but I put this in as my pattern and it gave me the desired effect, not sure why it was the * that functioned and not the +, maybe someone can clarify…

pattern="[a-z][A-Z][0-9]*

``````[a-z][A-Z][0-9]*
``````

Always (or nearly always) returns true as does,

``````[a-z][A-Z][0-9]+
``````

This pattern,

``````x = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/
console.log(x.test(`abc`))    //  false
console.log(x.test(`aBc`))    //  false
console.log(x.test(`aB0`))    //  true
console.log(x.test(`0aB`))    //  true
``````

1 Like

I like to use the following site: https://regexr.com/ when trying to come up with the correct regex pattern and to ensure it works as expected. If you hover over each part of the expression, it’ll tell you what it does.

So for the +, the site says:

Match 1 or more of the preceding token

1 Like

In the example given on this page, the type attribute “text” is used to fill out a credit card number, and a pattern attribute with [0-9]{14,16} is used. Why wasnt it instead done as type=“number” min=“0” max"9" minlength=“14” maxlength=“16” , which essentially means the same thing?

Without the “+” in “[a-zA-Z0-9]+” you are simply telling your code that letters “aA-Cc” and numbers ranging from “0-9” should appear only once in any given username. What this means is that a username like “cecelia” (note the double “c”) will render a validation message “Please match the requested format” as any username with a double-digit i.e 00, 33 etc.

The “+” sign placed at the end of the attribute allows for multiple occurrences of your valid set patterns.

1 Like

the “+” gives instruction to accept the input even if he repeats an alphabet as many times but within the range we set…whereas in the case of [o-9]{13-16} its not necessary cuz…obviously u cant enter a min of 13 digits without repeating a digit… i hope u understood this

1 Like

so without the + sign what does it mean… cos I don’t still understand please…

I am also not 100% confident in the exact function of the “+” sign after the brackets. Perhaps the following example would help illustrate it?

In Canada, we use Postal Code instead of Zip Code. It is a 6-character alternating letter-number combination such as A5A5A5. This pattern is followed 100% of the time nation-wide. How would I validate the user input in this case?

It is actually seven characters, not six because there is a space character in the middle.

``````T9W 1T1
``````

https://regexlib.com/REDetails.aspx?regexp_id=1293

1 Like

Thank you for the correction and reference, @mtf. So, this example does not use the ‘+’ function.
I.e.: Canadian Postal Codes, following the pattern of “A1A 1A1”, would use the regex of:
“([ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ])\ ?([0-9][ABCEGHJKLMNPRSTVWXYZ][0-9])”

1 Like

There are no quantifiers in that pattern since each position allows only one member of each class. The entire expression has two groups. Everything in the group must appear in the exact order the pattern is written. The two groups are separated by exactly one space character.

1 Like

What’s the difference between quantifiers and `minLength` / `maxLength`?
Why do we need both?

Quantifiers are symbols that denote a set or open range. 0 or 1, exactly 1, 0 or more, 1 or more.

`min` and `max` narrow the field to within a certain range both top and bottom. There is no open end when there is a max specified.