For only alpha numeric characters I use something like this:
<input type="text" pattern="[a-zA-Z0-9]+" />
When the data reaches the server I need to validate again. So I did
preg_match("[a-zA-Z0-9]+", $value);
But PHP says Warning: preg_match() [function.preg-match]: Unknown modifier ‘+’
Can’t you use the same expressions in html5 and PHP?
Edit:
I have a JSfiddle showing the suggestions below don’t work:
http://jsfiddle.net/EY3hc/
What I want to achieve is 1 regex I can use as pattern in html and in preg_match. The html is generated by PHP so I’d like to have one place to maintain code for validation.
You need to add delimiters in your expression. Also you should match the beginning and end of the line:
Without
^and$, an input likesomewords!@#would validate.