validation max-min pattern input

How do I put 2 words minimum and 2 words maximum in an input with a pattern?

<input type="text" name="name"; pattern="()">

So this field should have 2 words mandatory and 2 words max

Add Comment
2 Answer(s)

Try this:

<input pattern="^\w+\s\w+$"> 
Add Comment

I don’t know if plain html allows you to do that, but with the help of javascript it can be done, here’s a related answer:

Set maximum number of words HTML textbox

To add minimum amount of words, just change the logic a little bit:

if(numWords > maxWords && numWords < minWords) { ... 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.