a textarea is a part of my form. Th开发者_如何学JAVAe user has to write a little text and I want to validate this text. For now I am using the following regex:
/^[0-9a-zA-ZäöüÄÖÜ_\-']+$/
Although I have mentioned the äöüÄÖÜ in the regex it handles all words with äöü.. as invalid. Furthermore it does not accept empty spaces.
Any ideas how to improve the regex?
Use a Unicode-aware regex:
/[\pL\pN_\-]+/
the PCRE u modifier allows for utf-8. You are also missing a space from the regex, and you can condense it a bit:
/^[0-9a-zäöü\- ]+$/ui
Though I'm not sure if 'i' will work with the capitals of the foreign characters.
You may also want to include punctuation.
First, you might have an encoding issue, that's why äöüÄÖÜ are registered as invalid. I'm not a PHP user, so I can't answer your question directly, but taking a look at this page might help you. Also, using appropriate character classes could work better than explicitly writing all appropriate letters. Alas, this is also probably encoding configuration dependent.
Second, you need a space in your regex, so
/^[0-9a-z A-ZäöüÄÖÜ_\-']+$/ // note space after a-z
should work. Note what I wrote in last paragraph about using character classes. \w
might be sufficient instead of a-zA-ZäöüÄÖÜ
You may just use \w
to indicate all "word" characters (letters, digits, etc.) So the regex will be
/^[\w_\-' ]+$/
What text from the user are you considering to be "valid"?
精彩评论