tags:

views:

195

answers:

4
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.

+7  A: 

Your regex uses PCRE syntax, so you have to use preg_match() instead of eregi().

Try this code instead:

else if (!preg_match("/^([0-9a-z_\[\]* -])+$/i", $subuser)) {
$form->setError($field, "* Username not alphanumeric");
}
Henning
+1  A: 

even using preg_* functions the pattern needs to be wrapped in nonalphanum delimiters:

"~^([0-9a-z_[]*- ])+$~"

+3  A: 

Don't use the ereg family of functions - they are slower and, if I recall correctly, will eventually be deprecated.

This should fix it

if ( preg_match( "/^[^0-9a-z_\[\]* -]$/i", $subuser )
{
    $form->setError( $field, "* Username not alphanumeric" );
}
Peter Bailey
yep - they are going to be removed altogether in PHP6. Best to avoid them.
nickf
+4  A: 

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_[* -])+$"

Trent