I'm trying to write a preg_match function that matches invalid characters in an input (in this case, a username). Here is the function:
if ( preg_match('/[^A-Za-z0-9]/', $username) ) { error; }
Based on the regex, the if-statement should return true when it matches a character other than A-Z, a-z and 0-9 meaning that it found at least one match of an invalid character.
However, when I set $username equal to "ABCD&" or "ABCD+" excluding quotes, the if-statement never becomes true (no matches found), when it should match the & and + characters. Of course, "ABCD" also will not yield and error.
I am unsure of what the error is, but I presume I am missing a flag. The input comes from $_POST, and I do not apply any functions (such as addslashes or htmlspecialchars) to the input when I run that if-statement on it.
if ( preg_match('/[^A-Za-z0-9]/', $username) ) { error; }
Based on the regex, the if-statement should return true when it matches a character other than A-Z, a-z and 0-9 meaning that it found at least one match of an invalid character.
However, when I set $username equal to "ABCD&" or "ABCD+" excluding quotes, the if-statement never becomes true (no matches found), when it should match the & and + characters. Of course, "ABCD" also will not yield and error.
I am unsure of what the error is, but I presume I am missing a flag. The input comes from $_POST, and I do not apply any functions (such as addslashes or htmlspecialchars) to the input when I run that if-statement on it.