I use the following regex to validate a username (input type text in a registration form) in order to make sure that the username contains ONLY alphanumeric characters, dot, dash or underscore.
if (!preg_match('/^[a-zA-Z0-9\.\_-]+$/',$my_name)) { echo 'no_valid'; }
When I type in the text field for instance % or # or @ I get back correctly the error message that it's not a valid username, also the valid characters (.-_) are accepted, so it seems to work fine until the time I type & or +, then I can type any invalid character that I have already exclude before by using the preg_match.
Could anyone tell me why is this happening and how can I overcome this issue?
index.php?name=bob&xgives name asbob. You may want to add a minimum length ie/^[a-zA-Z0-9\._\-]{5,}$/for string of 5 chars or more&and+altogether, or do apreg_replace? It's an option.