2011-03-28 14:21:28 +00:00
|
|
|
<?
|
|
|
|
/*-- TODO ---------------------------//
|
|
|
|
Writeup how to use the VALIDATE class, add in support for form id checks
|
|
|
|
Complete the number and date validation
|
|
|
|
Finish the GenerateJS stuff
|
|
|
|
//-----------------------------------*/
|
|
|
|
|
|
|
|
class VALIDATE {
|
2013-06-18 08:00:48 +00:00
|
|
|
var $Fields = array();
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-06-18 08:00:48 +00:00
|
|
|
function SetFields($FieldName, $Required, $FieldType, $ErrorMessage, $Options = array()) {
|
2013-04-13 08:00:19 +00:00
|
|
|
$this->Fields[$FieldName]['Type'] = strtolower($FieldType);
|
|
|
|
$this->Fields[$FieldName]['Required'] = $Required;
|
|
|
|
$this->Fields[$FieldName]['ErrorMessage'] = $ErrorMessage;
|
|
|
|
if (!empty($Options['maxlength'])) {
|
|
|
|
$this->Fields[$FieldName]['MaxLength'] = $Options['maxlength'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['minlength'])) {
|
|
|
|
$this->Fields[$FieldName]['MinLength'] = $Options['minlength'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['comparefield'])) {
|
|
|
|
$this->Fields[$FieldName]['CompareField'] = $Options['comparefield'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['allowperiod'])) {
|
|
|
|
$this->Fields[$FieldName]['AllowPeriod'] = $Options['allowperiod'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['allowcomma'])) {
|
|
|
|
$this->Fields[$FieldName]['AllowComma'] = $Options['allowcomma'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['inarray'])) {
|
|
|
|
$this->Fields[$FieldName]['InArray'] = $Options['inarray'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Options['regex'])) {
|
|
|
|
$this->Fields[$FieldName]['Regex'] = $Options['regex'];
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ValidateForm($ValidateArray) {
|
|
|
|
reset($this->Fields);
|
|
|
|
foreach ($this->Fields as $FieldKey => $Field) {
|
2013-04-13 08:00:19 +00:00
|
|
|
$ValidateVar = $ValidateArray[$FieldKey];
|
|
|
|
|
|
|
|
if ($ValidateVar != '' || !empty($Field['Required']) || $Field['Type'] == 'date') {
|
|
|
|
if ($Field['Type'] == 'string') {
|
|
|
|
if (isset($Field['MaxLength'])) {
|
|
|
|
$MaxLength = $Field['MaxLength'];
|
|
|
|
} else {
|
|
|
|
$MaxLength = 255;
|
|
|
|
}
|
|
|
|
if (isset($Field['MinLength'])) {
|
|
|
|
$MinLength = $Field['MinLength'];
|
|
|
|
} else {
|
|
|
|
$MinLength = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($ValidateVar) > $MaxLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) < $MinLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'number') {
|
|
|
|
if (isset($Field['MaxLength'])) {
|
|
|
|
$MaxLength = $Field['MaxLength'];
|
|
|
|
} else {
|
|
|
|
$MaxLength = '';
|
|
|
|
}
|
|
|
|
if (isset($Field['MinLength'])) {
|
|
|
|
$MinLength = $Field['MinLength'];
|
|
|
|
} else {
|
|
|
|
$MinLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$Match = '0-9';
|
|
|
|
if (isset($Field['AllowPeriod'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Match .= '.';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (isset($Field['AllowComma'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Match .= ',';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/[^'.$Match.']/', $ValidateVar) || strlen($ValidateVar) < 1) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif ($MaxLength != '' && $ValidateVar > $MaxLength) {
|
|
|
|
return $Field['ErrorMessage'].'!!';
|
|
|
|
} elseif ($ValidateVar < $MinLength) {
|
|
|
|
return $Field['ErrorMessage']."$MinLength";
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'email') {
|
|
|
|
if (isset($Field['MaxLength'])) {
|
|
|
|
$MaxLength = $Field['MaxLength'];
|
|
|
|
} else {
|
|
|
|
$MaxLength = 255;
|
|
|
|
}
|
|
|
|
if (isset($Field['MinLength'])) {
|
|
|
|
$MinLength = $Field['MinLength'];
|
|
|
|
} else {
|
|
|
|
$MinLength = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match("/^".EMAIL_REGEX."$/i", $ValidateVar)) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) > $MaxLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) < $MinLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'link') {
|
|
|
|
if (isset($Field['MaxLength'])) {
|
|
|
|
$MaxLength = $Field['MaxLength'];
|
|
|
|
} else {
|
|
|
|
$MaxLength = 255;
|
|
|
|
}
|
|
|
|
if (isset($Field['MinLength'])) {
|
|
|
|
$MinLength = $Field['MinLength'];
|
|
|
|
} else {
|
|
|
|
$MinLength = 10;
|
|
|
|
}
|
|
|
|
|
2013-06-11 08:01:24 +00:00
|
|
|
if (!preg_match('/^'.URL_REGEX.'$/i', $ValidateVar)) {
|
2013-04-13 08:00:19 +00:00
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) > $MaxLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) < $MinLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'username') {
|
|
|
|
if (isset($Field['MaxLength'])) {
|
|
|
|
$MaxLength = $Field['MaxLength'];
|
|
|
|
} else {
|
|
|
|
$MaxLength = 20;
|
|
|
|
}
|
|
|
|
if (isset($Field['MinLength'])) {
|
|
|
|
$MinLength = $Field['MinLength'];
|
|
|
|
} else {
|
|
|
|
$MinLength = 1;
|
|
|
|
}
|
|
|
|
|
2013-06-11 08:01:24 +00:00
|
|
|
if (!preg_match('/^'.USERNAME_REGEX.'$/i', $ValidateVar)) {
|
2013-04-13 08:00:19 +00:00
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) > $MaxLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
} elseif (strlen($ValidateVar) < $MinLength) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'checkbox') {
|
|
|
|
if (!isset($ValidateArray[$FieldKey])) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'compare') {
|
2013-06-18 08:00:48 +00:00
|
|
|
if ($ValidateArray[$Field['CompareField']] != $ValidateVar) {
|
2013-04-13 08:00:19 +00:00
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'inarray') {
|
|
|
|
if (array_search($ValidateVar, $Field['InArray']) === false) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($Field['Type'] == 'regex') {
|
|
|
|
if (!preg_match($Field['Regex'], $ValidateVar)) {
|
|
|
|
return $Field['ErrorMessage'];
|
|
|
|
}
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // while
|
|
|
|
} // function
|
|
|
|
|
|
|
|
function GenerateJS($FormID) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ReturnJS = "<script type=\"text/javascript\" language=\"javascript\">\r\n";
|
|
|
|
$ReturnJS .= "//<![CDATA[\r\n";
|
|
|
|
$ReturnJS .= "function formVal() {\r\n";
|
|
|
|
$ReturnJS .= " clearErrors('$FormID');\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
|
|
|
reset($this->Fields);
|
|
|
|
foreach ($this->Fields as $FieldKey => $Field) {
|
2013-04-13 08:00:19 +00:00
|
|
|
if ($Field['Type'] == 'string') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value == ""';
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Field['MaxLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['MinLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'number') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Match = '0-9';
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Field['AllowPeriod'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Match .= '.';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['AllowComma'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Match .= ',';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^'.$Match.']/) || $(\'#'.$FieldKey.'\').raw().value.length < 1';
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Field['MaxLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 > '.$Field['MaxLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['MinLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value/1 < '.$Field['MinLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'email') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if (!validEmail($(\'#'.$FieldKey.'\').raw().value)';
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Field['MaxLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['MinLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 6';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'link') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if (!validLink($(\'#'.$FieldKey.'\').raw().value)';
|
2013-04-13 08:00:19 +00:00
|
|
|
if (!empty($Field['MaxLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > 255';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['MinLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < 10';
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'username') {
|
|
|
|
$ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value.match(/[^a-zA-Z0-9_\-]/)';
|
|
|
|
if (!empty($Field['MaxLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length > '.$Field['MaxLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
|
|
|
if (!empty($Field['MinLength'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'\').raw().value.length < '.$Field['MinLength'];
|
2013-04-13 08:00:19 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= ') { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'regex') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if (!$(\'#'.$FieldKey.'\').raw().value.match('.$Field['Regex'].')) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'date') {
|
|
|
|
$DisplayError = $FieldKey.'month';
|
|
|
|
if (isset($Field['MinLength']) && $Field['MinLength'] == 3) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$Day = '$(\'#'.$FieldKey.'day\').raw().value';
|
|
|
|
$DisplayError .= ",{$FieldKey}day";
|
2013-04-13 08:00:19 +00:00
|
|
|
} else {
|
|
|
|
$Day = '1';
|
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$DisplayError .= ",{$FieldKey}year";
|
|
|
|
$ValItemHold = ' if (!validDate($(\'#'.$FieldKey.'month\').raw().value+\'/\'+'.$Day.'+\'/\'+$(\'#'.$FieldKey.'year\').raw().value)) { return showError(\''.$DisplayError.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
if (empty($Field['Required'])) {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if ($(\'#'.$FieldKey.'month\').raw().value != ""';
|
|
|
|
if (isset($Field['MinLength']) && $Field['MinLength'] == 3) {
|
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'day\').raw().value != ""';
|
|
|
|
}
|
|
|
|
$ValItem .= ' || $(\'#'.$FieldKey.'year\').raw().value != "") {'."\r\n";
|
|
|
|
$ValItem .= $ValItemHold;
|
|
|
|
$ValItem .= " }\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem .= $ValItemHold;
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'checkbox') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if (!$(\'#'.$FieldKey.'\').checked) { return showError(\''.$FieldKey.'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
} elseif ($Field['Type'] == 'compare') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = ' if ($(\'#'.$FieldKey.'\').raw().value!=$(\'#'.$Field['CompareField'].'\').raw().value) { return showError(\''.$FieldKey.','.$Field['CompareField'].'\',\''.$Field['ErrorMessage'].'\'); }'."\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 08:00:19 +00:00
|
|
|
if (empty($Field['Required']) && $Field['Type'] != 'date') {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ReturnJS .= ' if ($(\'#'.$FieldKey.'\').raw().value!="") {'."\r\n ";
|
|
|
|
$ReturnJS .= $ValItem;
|
|
|
|
$ReturnJS .= " }\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
} else {
|
2013-06-23 08:00:54 +00:00
|
|
|
$ReturnJS .= $ValItem;
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
2013-06-23 08:00:54 +00:00
|
|
|
$ValItem = '';
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 08:00:54 +00:00
|
|
|
$ReturnJS .= "}\r\n";
|
|
|
|
$ReturnJS .= "//]]>\r\n";
|
|
|
|
$ReturnJS .= "</script>\r\n";
|
2011-03-28 14:21:28 +00:00
|
|
|
return $ReturnJS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|