<?php
/** This script demonstrates the usage of the Validator class **/
// Step 1: include the class file
include("validator.class.php");
// Step 2: create an instance of the Validator class. The parameter is either
// "GET" or "POST" depending on how your form has been submitted.
$v = new Validator("GET");
// Step 3: start checking! For more examples see below...
// returns zero if parameter 'x' is not a valid (positive) number
//$x = $v->is_valid_or_zero("x");
// returns zero if parameter 'x' is not a valid integer (positive or negative)
//$x = $v->is_valid_or_zero("x", vINTEGER);
// returns an empty string if the parameter is not a valid e-mail adress, so
// your script reacts as if the user hadn't entered an adress in the first place.
//$x = $v->is_valid_or_empty("x", vEMAIL);
?>
<html>
<head><title>Validator.class</title></head>
<body>
<form action="validatortest.php" method="GET">
<p>Enter any text, number or e-mail address:
<input name="var" type="input" size="20" maxlen="20" />
<input type="submit" name="submit" />
</p>
</form>
<?php
if(isset($_GET["var"])) {
echo "<p>You've entered: {$_GET["var"]}</p><ul>";
// returns 0 if parameter is not a valid number
$test = $v->get_valid_or_zero("var");
if($test == 0)
echo "<li><b>this is no valid number!</b>";
else
echo "<li>it's a valid number";
// returns empty string if parameter contains anything else than just letters
$test = $v->get_valid_or_empty("var", vALPHA);
if($test == "")
echo "<li><b>this string contains more than just letters!</b>";
else
echo "<li>it only contains letters, no numbers or punctuation marks";
// is parameter a valid name for a variable?
if($v->get_valid_or_empty("var", vVARIABLE))
echo "<li>it is a valid name for a variable";
else
echo "<li><b>it can't be used as a variable name</b>";
// returns empty string if parameter is no valid e-mail adress
$test = $v->get_valid_or_empty("var", vEMAIL);
if($test == "")
echo "<li><b>this is no valid e-mail adress</b>";
else
echo "<li>it's a valid e-mail adress";
// returns zero if value isn't between 20 and 30 or if it isn't a valid number
$test = $v->get_in_range_or_zero("var", 20, 30);
if($test == 0)
echo "<li><b>this isn't a number between 20 and 30</b>";
else
echo "<li>it's a number and it's between 20 and 30";
}
?>
</body>
</html>
|