--- nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc 2004/05/06 16:27:22 1.3 +++ nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc 2005/08/11 14:09:26 1.4 @@ -1,7 +1,7 @@ + * @author Walter A. Boring IV * @package phpHtmlLib * @subpackage FormProcessing * @@ -38,7 +38,7 @@ var $_error_code = ''; /** - * This holds the last error message + * This holds the last error message */ var $_error_message = ''; @@ -58,9 +58,27 @@ function FormValidation(&$error_obj) { $this->_FormErrors = &$error_obj; } - - - + + + /** + * A singleton method for only + * creating one instance of this + * per request. + * + * @return FormValidation object + */ + function &singleton() { + static $obj=NULL; + + if (is_null($obj)) { + $obj = new FormValidation(new FormErrors); + } + + return $obj; + } + + + /** * This method checks to make sure an array doesn't have * an empty element. @@ -95,7 +113,7 @@ } else { $this->_error_message = $message; } - + return FALSE; } @@ -111,8 +129,8 @@ } /** - * This method returns the error - * message from the last validation + * This method returns the error + * message from the last validation * error found. * * @return string @@ -135,7 +153,7 @@ } return TRUE; } - + /** * This function checks if the given string contains * alphabetical characters or numbers. @@ -145,7 +163,7 @@ * @param string - list of individual characters to allow * besides a-zA-Z * - * @return boolean FALSE - if any other extraneous + * @return boolean FALSE - if any other extraneous * characters are found * TRUE upon succes. */ @@ -179,7 +197,7 @@ * @param string - list of individual characters to allow * besides a-zA-Z * - * @return boolean FALSE - if any other extraneous + * @return boolean FALSE - if any other extraneous * characters are found * TRUE upon succes. */ @@ -190,13 +208,18 @@ return FALSE; } + //now do another simple check + if ($from == '' && !is_numeric($str)) { + return FALSE; + } + $to = ""; $len = strlen($from); for ( $i = 0; $i < $len; ++$i ) { $to .= "0"; } $str = strtr($str, $from, $to); - $substr = eregi_replace("[0-9]+", "", $str); + $substr = eregi_replace("[-0-9]+", "", $str); if ( !$this->array_hasempty($substr) ) { return FALSE; } @@ -222,7 +245,7 @@ /** * This method makes sure a value lies within - * a given range of values. + * a given range of values. * The error message can be customized by passing in * a customer error code * @@ -254,7 +277,7 @@ } return TRUE; } - + /** * This method validates a string as containing * only letters and numbers @@ -570,7 +593,7 @@ } /** - * This method validates a string as a + * This method validates a string as a * path to a file. * * @param string - the value to validate @@ -599,7 +622,7 @@ } return TRUE; } - + /** * This validates a string as a valid proper name. @@ -611,7 +634,7 @@ */ function is_name ($name) { //letters & numbers only - if ( !$this->is_alphanum($name, ".- '") ) { + if ( !$this->is_alphanum($name, ".- 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠšŸ") ) { return $this->_error("INVALID_NAME_CHARACTERS"); } // VALIDATE_MAXSIZE @@ -638,7 +661,7 @@ /** - * This method tests to see if a string value + * This method tests to see if a string value * is a valid 'account' name. The string can't * be larger then VALIDATE_MAXSIZE, and can only * contain alphanum characters @@ -677,9 +700,6 @@ if ( $len > VALIDATE_MAXSIZE ) { return $this->_error("INVALID_PASSWORD_LENGTH"); } - if ( !$this->is_alphanum($password) ) { - return $this->_error("INVALID_PASSWORD_CHARACTERS"); - } return TRUE; } @@ -721,9 +741,10 @@ * jane@blah.com or "Jane Doe " * * @param string - the value to validate + * @param bool - allows long email name format * @return TRUE = succes FALSE = failed */ - function is_email ($email) { + function is_email ($email, $allow_name = true) { //no quotes allowed if ( strstr($email, '"') || strstr($email, "'") ) { @@ -734,6 +755,11 @@ //"Foo Bar " $name = explode(" ", $email); if ( count($name) > 1 ) { + + if (!$allow_name) { + return $this->_error("INVALID_EMAIL"); + } + //it looks like they gave us an email //with a leading name such as Foo Bar //find the email address inside <> @@ -762,7 +788,7 @@ } // "@.", ".@.", "_@.", .com, .net, .edu, .blah - if ( !ereg("^( )*([a-zA-Z0-9_/:]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $email, $arr) ) { + if ( !ereg("^( )*([a-zA-Z0-9_/:]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$", $email, $arr) ) { return $this->_error("INVALID_EMAIL"); } @@ -787,7 +813,7 @@ * This function tests a string that may * contain many email addresses seperated by * commas - * + * * @param string - the value to validate * @return TRUE = succes FALSE = failed */ @@ -796,7 +822,7 @@ $email_arr = explode(",", $emails); foreach( $email_arr as $key => $email) { - $res = $this->is_email($email); + $res = $this->is_email(trim($email)); if ( $res !== TRUE ) { return $res; } @@ -806,7 +832,7 @@ /** - * This method validates a string as a + * This method validates a string as a * leap year. * * @param string - the value to validate @@ -900,14 +926,14 @@ /** - * This validates an array of values as a - * valid date time + * This validates an array of values as a + * valid date time * * NOTE: array must contain * array( * "month" => VALUE, * "day" => VALUE, - * "year" => VALUE, + * "year" => VALUE, * "hour" => VALUE, * "minutes" => VALUE, * "seconds" => VALUE); @@ -935,7 +961,7 @@ } /** - * This validates an array of fields as a + * This validates an array of fields as a * valid time of the day * * NOTE: array must contain @@ -1053,7 +1079,7 @@ } /** - * This method validates a string as a valid url + * This method validates a string as a valid url * It inclues the prefix, hostname/ip, port number * and path. *