0

The validation I need is the following A-Z, a-z, 0-9, no trailing/leading whitespace, -._ only once non-trailing/leading

Till now I have the following:

<input type="text" pattern="^\S[a-zA-Z0-9]*\S$">

This takes care of the leading trailing whitespace and A-Z,a-z,0-9, but it needs minimum 2 characters

Then I thought of something like this for the symbols -._

<input type="text" pattern="^\S[a-zA-Z0-9]*[\-_+]{,1}\S$">

But no luck there. Any ideas?

1
  • could you provide some examples for valid and invalid matches? Commented Dec 1, 2014 at 11:33

1 Answer 1

3

Since pattern attribute uses the same syntax as JavaScript, you can use the following regex:

<input type="text" pattern="^(?=..)[a-zA-Z0-9]*([_.-][a-zA-Z0-9]*)?$">

You can test with the snippet above by typing something in, then change focus from the input.

The input can only contain a-zA-Z0-9 and at most once of _.-. The look-ahead in front (?=..) checks that there are at least 2 characters.

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.