Skip to content
Advertisement

Can’t use certain characters in a regex

if(!eregi("^([0-9a-z_[]*- ])+$", $subuser))
    $form->setError($field, "* Username not alphanumeric");

Can anybody tell me why it is not allowing characters such as - and *?

if(!eregi("^([0-9a-z])+$", $subuser))
    $form->setError($field, "* Username not alphanumeric");

That is the original piece of code. A friend changed it to the top piece and it will allow a-z and 0-9 but it wont allow the other characters I need it to. Can anyone help me?

Thanks in advance.

Advertisement

Answer

For bracket expressions:

To include a literal ] in the list, make it the first character (following a possible ^). To include a literal -, make it the first or last character, or the second endpoint of a range. To use a literal - as the first endpoint of a range, enclose it in [. and .] to make it a collating element (see below). With the exception of these and some combinations using [ (see next paragraphs), all other special characters, including , lose their special significance within a bracket expression.

So this should do what you want:

"^([]0-9a-z_[* -])+$"
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement