Validating User Input
| From: | SP | Date: | Fri, 19 Apr 2002 12:57:34 +0000 |
| Subject: | Validating User Input | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-93679@lists.php.net to get a copy of this message | ||
I am trying to validate an user's input. I can get the ereg function to
work if I just type in the pattern I'm searching for but my problem is I
want to build the pattern through a variable first and then use that
variable in the ereg function.
For example, I want to check an input that's only text and only between 5 to
20 characters in length. Is this possible?
function check_input($user_input, $min=0, $max=0, $text=false,
$number=false, $special=false, $default="")
{
if ($text) $pattern .= "a-zA-Z";
if ($number) $pattern .= "0-9";
if ($special) $pattern .= "[:space:]";
if (ereg("^[$pattern]{$min,$max}$", $user_input))
return $user_input;
else
return $default;
}
Also, which of the following special characters is considered safe to
accept? I am just allowing spaces now but would like as many of the below
characters to be included.
~ ` ! @ # $ % ^ & * ( ) _ + - = [ ] \ { } | : " ; ' < > ? , . /