Page is a not externally linkable
rocknbil - 7:58 pm on Dec 14, 2009 (gmt 0)
First, is zero a valid digit in your scenario? If it's "anything but zero" you don't need a preg. is_numeric() works, but that includes zero. if ($var > 0) { You can use is_numeric if zero is included, or Do this first to make sure user hasn't errantly included a space or touched some other key: then if (preg_match('/^\d+$/',$var)) { begins and ends with any number of digits if (preg_match('/^\d$/',$var)) { begins and ends with exactly one digit if (preg_match('/^\d{2}$/',$var)) { begins and ends with exactly two digits if (preg_match('/^\d{4,7}$/',$var)) { begins and ends with anywhere from four to seven digits Try this. // top of script for ease of use: $isValid=1;
i have a preg_match to check to see if value of $id is a digit like this:
// simple enough
}
$var = preg_replace('/[^\d]+/','',$var); i am using the following code
$bannedNames = Array (
'secret',
'x',
'spam-boy'
);
// This is anything NOT these characters; that is,
// it will throw away anything NOT in this list.
// No need for a-z, we will use case insensitive modifier.
$allowed_chars = 'A-Z0-9\-_'; // Escape dash, not a range
$name=preg_replace("/[^$allowed_chars]+/i",$_POST['new_user_name']); // Do this first!
foreach ($bannedNames as $nm) {
if (preg_match("/^$nm$/i",$name)) { $isValid=0; } // note case insensitive
if ($isValid==0) { break; }
}
if ($isValid==0) {
echo "Please use the message board properly and enter a valid name. Inappropriate use will result in a ban. ";
exit;
}