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

Not quite… If we exchange the “minlength & maxlength” attributes for {3,15}, then yes, the form will not allow you to submit, and you will be required to match the correct format, but it will allow you to type as many characters as you want into the text field. If you leave the “minlength” & “maxlength” attributes, then the field does not allow more than 15 characters to be typed into the field.

7 Likes

Hello, As we make the password required how do we make it have at least one uppercase character and a symbol. Also, how do we allow “[a-zA-Z0-9]+” to accept characters as well?

I am also confused by this. Why is the “+” necessary?

In this section, should we remove the required minlength="3" maxlength="15" and replace it with <input id="username" name="username" type="text" required pattern="[a-zA-Z0-9]{3,15}+"> ?

The same questions have already been responded in the thread.
+ means you have to input one or more characters.
@css7427613763 explained your other question pretty well. Removing min and max attributes will remove the limit of characters and instead will allow you to type in as many as you want, even though it still won’t let you submit it if you have {3,15} and the characters exceed 15.

3 Likes

Blockquote I’m not sure what would happen if you didn’t include anything after the class definition e.g. just [a-zA-Z0-9] . Why don’t you try it out and see what happens? I’m finding that I’m learning a lot by testing code using trial and error. I’ll try it out too and get back to you with my discovery. If neither of us can work it out, then maybe @mtf can clarify :wink:

I ran a few tests and if we leave it without the “+” it only accepts one character.

In the example [0-9]{14,16} it works because we define that it must match at least 14 digits and 16 digits maximum.

If it wasn’t for it, only one character would be accepted (regardless of repetition, I tried entering “A1” and it wasn’t accepted), that is why we would need the “+” sign.

4 Likes

Exactly what I ended up discovering :smiley:

Please share with us what you have learned along these lines.

Nothing new. I just wanted to confirm @methodrunner97513’s findings tie in with what I discovered back in February (the link in my post references back to that).

@el_escandalo I don’t know if you guys still have a doubt, but for any future users: as far as I understand, the ‘+’ just means ‘however many times’. So /a+/ means that it will match any number of a’s between 1 and infinite — hence {1, }, where the empty spot signifies ‘infinite’.

1 Like

Hi, just want to extend on your answer.
If we use: [0-9]{14,16},
Then the we can omit the elements: minlength=“14” maxlength=“16”?
Because they seem to do the same thing, right?

Not sure that is something we can write into a pattern, but it is what the quantifiers represent.

From reading through this thread and playing around they are similar but not the same.

minlength
when the minimum length is not met and you click outside the input box then hover your mouse over it you will be prompted to enter the minimum number of characters. If you try to submit you will receive and error message that tells you the minimum number of characters you must enter (and how many you tried to enter).

maxlength
When the maximum number of characters are reached it will not let you enter any more characters. There is no submission error from having too many characters as you simply can’t enter more than the maxlength in this input field in the first place.

pattern="[0-9]{3,8}"
minimum submission is three characters maximum submission is 8 characters. This does not limit how many characters can be input into the field. It only limits what can be submitted.
Any breach of any aspect of the pattern attribute seems to result in the same error message
“please match the requested format”.
Clicking outside the input field and hovering the mouse over the field will bring up the same message as a prompt.

The error message from the pattern attribute seems to overrides that of the minlength error message.

Thus if you have minlength and maxlength set correctly then you should use + or * in the pattern attribute.

Unless including some combination of patterns makes the pattern a better place to set the minimum or maximum length. That’s beyond me at the moment. In this simple format though minlength and maxlength seem to be the better attributes as the feedback is more clear and specific if the user gets it wrong.

3 Likes

@mtf I have tried a few entries just using three blank spaces as a user name and swapping the quantifier from a + to a * without any errors or changes. There doesn’t seem to be any obvious difference between the two. What am I not considering or testing?

This explanation helped the most thank you!