Hi,
<?php
function check_input($user_input, $min=0, $max=0, $text=false,
$number=false, $special=false, $default="")
{
$pattern="";
if ($text) $pattern.='a-zA-Z';
if ($number) $pattern.='0-9';
if ($special)
$pattern.='[:space:]\~\`\@\#\$\%\^\&\*\(\)\_\+\-\=\\\{\}\|\:\"\;\'\<\>\?\,\.
\/';
$regexp='^['.$pattern.']{'.$min.','.$max.'}$';
if (ereg($regexp,$user_input))
return $user_input;
else
return $default;
}
?>
That should take care of everything except the square brackets - not sure
how to go about getting them (escaping them didn't seem to work).
HTH
Danny.
----- Original Message -----
From: "SP" <[EMAIL PROTECTED]>
To: "Php" <[EMAIL PROTECTED]>
Sent: Friday, April 19, 2002 1:57 PM
Subject: [PHP] Validating User Input
> 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.
>
> ~ ` ! @ # $ % ^ & * ( ) _ + - = [ ] \ { } | : " ; ' < > ? , . /
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php