0

I have a single input where I need the user to type the first and last name. But I need a validation, I need to check that the first two words have at least three characters.

For example, if I type "Joe Do" or "Jo Doe" or even "Jo Do" I return a boolean value with the value false.

I tried using the following regex:

isValidName= /^(\w{3,20})(\s)(\w{3,20})$/g.test(name)

However, if I type a third string the validation stops working.

I have little knowledge with regex, so thanks in advance for any kind of help.

1 Answer 1

1

The problem with your regex is that you're anchoring the end of the second word with a $, which forces a matching string to have exactly two words. You can use \b to assert a word boundary instead at the end of the second word:

^\w{3,20}\s\w{3,20}\b
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.