19

I need a pattern for a HTML5 form.

  • Lowercase letters accepted
  • Uppercase letters accepted
  • Numbers accepted
  • Minus character accepted

HTML so far

<input type="text" pattern="[a-zA-Z0-9-]" required>

The code above does not work. I guess I'm close?

1 Answer 1

56

You are pretty close actually:

[a-zA-Z0-9-]+
            ^

You just needed this + plus quantifier, which tells the input field to accept one or more the characters you have listed in the character class [].

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

2 Comments

How do you tell you want at least a specific number of characters?
Hi everyone, when I am trying to use this example, I am getting following error in Chrome - Pattern attribute value [a-zA-Z0-9-]+ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-zA-Z0-9-]+/v: Invalid character class Adding backslash before dash fix it: [a-zA-Z0-9\-]+

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.