|
Posted by Hilarion on 11/29/05 17:02
> I want to check the input by a form field by PHP. I only want to allow
> capital or small letters, numbers, a - and a space. Now I use
> "[^A-Za-z0-9-]" . What do I need to add to allow spaces?
Shouldn't it have the "^" sign before the "["? Now it allows
everything beside letters, digits and a minus sign. You
should also add "*" after the "]" to allow not only one valud
character, but any number of it, and add "$" sign at the end
to prevent matching text that starts with valid characters,
but then contains invalido ones.
You can add space by prefixing it with backslash.
If you are using single quotes for the expression, then it should
be:
'^[\ A-Za-z0-9-]*$'
If you are using double quotes, then the backslash should be
escaped (to make it a part of the string, and not a PHP way
of escaping special character) by prefixing it with another
one (and you probably should escape the "$" sign too):
"^[\\ A-Za-z0-9-]*\$"
As you can see it's easier to use single quotes around regular
expressions.
Hilarion
Navigation:
[Reply to this message]
|