How do you check if a username contains invalid characters?
I want to restrict each users username with PHP to having numbers, letters, and underscores.
How do you check if a username contains invalid characters?
I want to restrict each users username with PHP to having numbers, letters, and underscores.
You can use a regular expression:
if (preg_match("^[0-9A-Za-z_]+$", username) == 0) {
echo "<p>Invalid username</p>";
}
Try:
<?php
function IsSafe($string)
{
if(preg_match('/[^a-zA-Z0-9_]/', $string) == 0)
{
return true;
}
else
{
return false;
}
}
?>