Regex, numerals and using variables for bounds
Posted: Tue Jun 08, 2010 10:42 am
The following code works nicely:
However I'm having real trouble with this parallel function:
If I go into the regexp and use '1' and '5' rather than $minVal and $maxVal, it works. But it doesn't work using the variables, even tho the parallel function validateNames() does work using the variables. I keep getting the "Not valid input" response.
What am I missing?
Code: Select all
function validateNames($str2val, $minVal, $maxVal)
{
$pattern = '/^[a-zA-Z-,\'.& ]{'.$minVal.',' .$maxVal.'}$/';
if(preg_match($pattern, $str2val)) return True;
else return False;
}
$str2val = $_POST['theName'];
$minVal = 1;
$maxVal = 5;
$check = validateNames($str2val, $minVal, $maxVal);
if ($check === True) echo "Validated";
else echo "Not valid input";
Code: Select all
function validateNumbers($str2val, $minVal, $maxVal)
{
$pattern = '/^[0-9]{'.$minVal.', '.$maxVal.'}$/';
if(preg_match($pattern, $str2val)) return True;
else return False;
}
$minVal = 1;
$maxVal = 5;
$str2val = (int)$_POST['theName'];
$check = validateNumbers($str2val,$minVal, $maxVal);
if ($check === True) echo "Validated";
else echo "Not valid input";
What am I missing?