Reputation: 127
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.
Upvotes: 1
Views: 509
Reputation: 56
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_[* -])+$"
Upvotes: 4
Reputation: 105888
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" );
}
Upvotes: 3
Reputation:
even using preg_* functions the pattern needs to be wrapped in nonalphanum delimiters:
"~^([0-9a-z_[]*- ])+$~"
Upvotes: 1
Reputation: 11696
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");
}
Upvotes: 7