0

I need to validate a string from input in an HTML form with Javascript RegEx.

There may be an unlimited amount of "words" (any combination of letters and numbers - [a-zA-Z0-9], no symbols, underscores, dashes, hyphens) separated with a single space (" ", not mandatory after the last "word", or the first one, if there is only one). Minimal length of each of these "words" is 3 symbols, maximal is 10 symbols.

For example, string which could be valid - "bob BoB4U BOB4EVER 112233".

Presence any of the following "words" should make a string invalid: bo, b@b, bobobobobobobob, bob-4-u, b_o_b.

Have found RegEx for a length of the string: ^(?=[\S\s]{3,10}$)[\S\s]*, tried to play with it.

Also tried to combine the previous with something like ^([a-zA-Z0-9]{3,10}\s?)*$, but alas, didn't manage.

UPDATE. Strings and words given above are just an EXAMPLE. I do not need to filter out those particular words, it's just for a visual representation of valid and invalid items.

0

1 Answer 1

1

There might be a cleaner way to write it, but a basic example would be zero or more times match it with a space and at least one with the end of the string.

var re = /^([a-z0-9]{3,10}\s)*[a-z0-9]{3,10}$/i;

Visualize it

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I tried almost the same and it didn't work. At first, I thought this one will not work either, but suddenly - it did. Hope it will come with experience)))

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.