Building An Extensible Form Validator Class - Floating Like A Butterfly
(Page 7 of 12 )
Two useful corollaries of the isNumber() method are the isInteger() and isFloat() methods, which provide for more precise data validation capabilities when dealing with numeric data.
<?php
class FormValidator
{
// snip
// check whether input is an integer
function isInteger($field, $msg)
{
$value = $this->_getValue($field);
if(!is_integer($value))
{
$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
return false;
}
else
{
return true;
}
}
// check whether input is a float
function isFloat($field, $msg)
{
$value = $this->_getValue($field);
if(!is_float($value))
{
$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
return false;
}
else
{
return true;
}
}
}
?>
Another very useful method, and one which I find very useful
when building forms which ask for the user's age, is the isWithinRange() method - it provides an easy way to check whether the input is within a certain numeric range.
<?php
class FormValidator
{
// snip
// check whether input is within a valid numeric range
function isWithinRange($field, $msg, $min, $max)
{
$value = $this->_getValue($field);
if(!is_numeric($value) || $value < $min || $value >
$max)
{
$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
return false;
}
else
{
return true;
}
}
}
?>
Next: Mail Dot Com >>
More PHP Articles
More By icarus, (c) Melonfire