I have an input box on my site and I want to validate that it is between 4 and 30 characters that are alphanumeric or any of . _ - (dot, underscore, hyphen).
e.g. these are valid:
- hello-
- goodbye23_.sd
not valid:
- boo
- nospecials%
- this has a space
In my html I have this line:
<input id="handletext" type="text" spellcheck="false" pattern="^[\w-\.]{4,30}$" maxlength="30" />
I get an error in the debugger when I load the page in latest Chrome:
Pattern attribute value ^[\w-.]{4,30}$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^[\w-.]{4,30}$/: Invalid character class.
Any ideas what's wrong with my pattern?
.:pattern="[\w.-]{4,30}". You do not need^and$, the anchors are added automatically by the HTML5 engine.