<?
class Validator {
var $errors; // A variable to store a list of error messages
// Validate something's been entered
function validateGeneral($theinput,$description = '') {
if (trim($theinput) != "") {
return true;
} else {
$this->errors[] = $description;
return false;
}
}
// Validate empty variabel
function validateEmpty($theinput,$description = '') {
if (empty($theinput)) {
$this->errors[] = $description;
return false;
} else {
return true;
}
}
// Validate text only
function validateTextOnly($theinput,$description = '') {
$result = ereg ("^[A-Za-z0-9\ ]+$", $theinput );
if ($result) {
return true;
} else {
$this->errors[] = $description;
return false;
}
}
// Validate text only, no spaces allowed
function validateTextOnlyNoSpaces($theinput,$description = '') {
$result = ereg ("^[A-Za-z0-9]+$", $theinput );
if ($result) {
return true;
} else {
$this->errors[] = $description;
return false;
}
}
// Validate email address
function validateEmail($themail,$allowempty,$description = '') {
if (!empty($themail)) {
$result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $themail );
if ($result) {
return true;
} else {
$this->errors[] = $description;
return false;
}
}
if ($allowempty = "yes") {
return true;
} else {
$this->errors[] = "epost adressen kan ikke være tom";
return false;
}
}
// Validate numbers only
function validateNumber($theinput,$description = '') {
if (is_numeric($theinput)) {
return true; // The value is numeric, return true
} else {
$this->errors[] = $description; // Value not numeric! Add error description to list of errors
return false; // Return false
}
}
// Validate date
function validateDate($thedate,$description = '') {
if (strtotime($thedate) === -1 || $thedate == '') {
$this->errors[] = $description;
return false;
} else {
return true;
}
}
// Check whether any errors have been found (i.e. validation has returned false)
// since the object was created
function foundErrors() {
if (count($this->errors) > 0) {
return true;
} else {
return false;
}
}
// List errors
function showErrors($title = 'The following errors occured:',$width = '100%') {
print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"$width\">".
"<tr><td bgcolor=\"#A12A2A\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"images/err.gif\" alt=\"info\" width=\"28\" height=\"32\"></td>".
"<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">".
"<span class=\"errmsg\">$title</span>".
"<ul>";
foreach ($this->errors as $errornr) {
print "<li class=\"errmsg\">".htmlentities($errornr)."</li>";
}
print "</ul>".
"</td></tr></table><br>";
}
// Manually add something to the list of errors
function addError($description) {
$this->errors[] = $description;
}
}
?>