RE: [PHP] Form Validating Class (OOP misunderstandings...)

From: Date: Tue, 21 Jan 2003 17:22:25 +0000
Subject: RE: [PHP] Form Validating Class (OOP misunderstandings...)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-132459@lists.php.net to get a copy of this message
Nicholas, I think I know where you found this code. I have the same code, except I modified it a little to work for me. You may need to change it for your needs. When I first used it, it didn't work. I had to modify it to make it work. My code is usually kind of sloppy, but maybe you would get better use of the following class: <?php class FormValidator { var $_errorList; function FormValidator() { $this->resetErrorList(); } function isEmpty($field, $msg) { if (trim($field) == "") { $this->_errorList[] = array("value" => $field, "msg" => $msg); return true; } else { return false; } } function isZero($field, $msg) { if($field == 0) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return true; } else { return false; } } function isUploadedFile($img, $img_type, $img_size, $msg) { $pattern = "/(pjpeg)$/"; if (is_uploaded_file($img) && preg_match($pattern, $img_type) && $img_size <= 1000000) { return true; } else { $this->_errorList[] = array("value" => $img, "msg" => $msg); return false; } } function isString($field, $msg) { if(!is_string($field)) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isNumber($field, $msg) { if(!is_numeric($field)) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isInteger($field, $msg) { if(!is_integer($field)) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isFloat($field, $msg) { if(!is_float($field)) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isWithinRange($field, $msg, $min, $max) { if(!is_numeric($field) || $field < $min || $field > $max) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isStringWithinRange($field, $msg, $min, $max) { if(strlen($field) < $min || strlen($field) > $max) { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } else { return true; } } function isAlpha($field, $msg, $special_chars="") { $pattern = "/^[a-zA-Z\s" . $special_chars . "]+$/"; if(preg_match($pattern, $field)) { return true; } else { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } } function isAlphaNum($field, $msg, $special_chars="") { $pattern = "/^[a-zA-Z0-9\s" . $special_chars . "]+$/"; if(preg_match($pattern, $field)) { return true; } else { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } } function isEmailAddress($field, $msg) { $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/"; if(preg_match($pattern, $field)) { return true; } else { $this->_errorList[] = array("value" => $field, "msg" => $msg); return false; } } function isError() { if (sizeof($this->_errorList) > 0) { return true; } else { return false; } } function getErrorList() { return $this->_errorList; } function resetErrorList() { $this->_errorList = array(); } } ?> >

« previous php.general (#132459) next »