/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc
ViewVC logotype

Diff of /nfo/php/libs/com.newsblob.phphtmllib/form/FormValidation.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by jonen, Thu May 6 16:27:22 2004 UTC revision 1.4 by jonen, Thu Aug 11 14:09:26 2005 UTC
# Line 9  Line 9 
9   * class, you create an array of fields that you want   * class, you create an array of fields that you want
10   * checked in the validate() method.   * checked in the validate() method.
11   *   *
12   * @author Walter A. Boring IV <waboring@buildabetterweb.com>   * @author Walter A. Boring IV <waboring@newsblob.com>
13   * @package phpHtmlLib   * @package phpHtmlLib
14   * @subpackage FormProcessing   * @subpackage FormProcessing
15   *   *
# Line 38  class FormValidation { Line 38  class FormValidation {
38      var $_error_code = '';      var $_error_code = '';
39    
40      /**      /**
41       * This holds the last error message       * This holds the last error message
42       */       */
43      var $_error_message = '';      var $_error_message = '';
44    
# Line 58  class FormValidation { Line 58  class FormValidation {
58      function FormValidation(&$error_obj) {      function FormValidation(&$error_obj) {
59          $this->_FormErrors = &$error_obj;          $this->_FormErrors = &$error_obj;
60      }      }
61        
62        
63                /**
64             * A singleton method for only
65             * creating one instance of this
66             * per request.
67             *
68             * @return FormValidation object
69             */
70            function &singleton() {
71                    static $obj=NULL;
72    
73                    if (is_null($obj)) {
74                            $obj = new FormValidation(new FormErrors);
75                    }
76    
77                    return $obj;
78            }
79    
80    
81    
82      /**      /**
83       * This method checks to make sure an array doesn't have       * This method checks to make sure an array doesn't have
84       * an empty element.       * an empty element.
# Line 95  class FormValidation { Line 113  class FormValidation {
113          } else {          } else {
114              $this->_error_message = $message;              $this->_error_message = $message;
115          }          }
116            
117          return FALSE;          return FALSE;
118      }      }
119    
# Line 111  class FormValidation { Line 129  class FormValidation {
129      }      }
130    
131      /**      /**
132       * This method returns the error       * This method returns the error
133       * message from the last validation       * message from the last validation
134       * error found.       * error found.
135       *       *
136       * @return string       * @return string
# Line 135  class FormValidation { Line 153  class FormValidation {
153          }          }
154          return TRUE;          return TRUE;
155      }      }
156        
157      /**      /**
158       * This function checks if the given string contains       * This function checks if the given string contains
159       * alphabetical characters or numbers.       * alphabetical characters or numbers.
# Line 145  class FormValidation { Line 163  class FormValidation {
163       * @param string - list of individual characters to allow       * @param string - list of individual characters to allow
164       *                 besides a-zA-Z       *                 besides a-zA-Z
165       *       *
166       * @return boolean FALSE - if any other extraneous       * @return boolean FALSE - if any other extraneous
167       *                         characters are found       *                         characters are found
168       *                 TRUE upon succes.       *                 TRUE upon succes.
169       */       */
# Line 179  class FormValidation { Line 197  class FormValidation {
197       * @param string - list of individual characters to allow       * @param string - list of individual characters to allow
198       *                 besides a-zA-Z       *                 besides a-zA-Z
199       *       *
200       * @return boolean FALSE - if any other extraneous       * @return boolean FALSE - if any other extraneous
201       *                         characters are found       *                         characters are found
202       *                 TRUE upon succes.       *                 TRUE upon succes.
203       */       */
# Line 190  class FormValidation { Line 208  class FormValidation {
208              return FALSE;              return FALSE;
209          }          }
210    
211            //now do another simple check
212            if ($from == '' && !is_numeric($str)) {
213                return FALSE;
214            }
215    
216          $to = "";          $to = "";
217          $len = strlen($from);          $len = strlen($from);
218          for ( $i = 0; $i < $len; ++$i ) {          for ( $i = 0; $i < $len; ++$i ) {
219              $to .= "0";              $to .= "0";
220          }          }
221          $str = strtr($str, $from, $to);          $str = strtr($str, $from, $to);
222          $substr = eregi_replace("[0-9]+", "", $str);          $substr = eregi_replace("[-0-9]+", "", $str);
223          if ( !$this->array_hasempty($substr) ) {          if ( !$this->array_hasempty($substr) ) {
224              return FALSE;              return FALSE;
225          }          }
# Line 222  class FormValidation { Line 245  class FormValidation {
245    
246      /**      /**
247       * This method makes sure a value lies within       * This method makes sure a value lies within
248       * a given range of values.         * a given range of values.
249       * The error message can be customized by passing in       * The error message can be customized by passing in
250       * a customer error code       * a customer error code
251       *       *
# Line 254  class FormValidation { Line 277  class FormValidation {
277          }          }
278          return TRUE;          return TRUE;
279      }      }
280        
281      /**      /**
282       * This method validates a string as containing       * This method validates a string as containing
283       * only letters and numbers       * only letters and numbers
# Line 570  class FormValidation { Line 593  class FormValidation {
593      }      }
594    
595      /**      /**
596       * This method validates a string as a       * This method validates a string as a
597       * path to a file.       * path to a file.
598       *       *
599       * @param string - the value to validate       * @param string - the value to validate
# Line 599  class FormValidation { Line 622  class FormValidation {
622          }          }
623          return TRUE;          return TRUE;
624      }      }
625        
626    
627      /**      /**
628       * This validates a string as a valid proper name.       * This validates a string as a valid proper name.
# Line 611  class FormValidation { Line 634  class FormValidation {
634       */       */
635      function is_name ($name) {      function is_name ($name) {
636          //letters & numbers only          //letters & numbers only
637          if ( !$this->is_alphanum($name, ".- '") ) {          if ( !$this->is_alphanum($name, ".- 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠšŸ") ) {
638              return $this->_error("INVALID_NAME_CHARACTERS");              return $this->_error("INVALID_NAME_CHARACTERS");
639          }          }
640          // VALIDATE_MAXSIZE          // VALIDATE_MAXSIZE
# Line 638  class FormValidation { Line 661  class FormValidation {
661    
662    
663      /**      /**
664       * This method tests to see if a string value       * This method tests to see if a string value
665       * is a valid 'account' name.  The string can't       * is a valid 'account' name.  The string can't
666       * be larger then VALIDATE_MAXSIZE, and can only       * be larger then VALIDATE_MAXSIZE, and can only
667       * contain alphanum characters       * contain alphanum characters
# Line 677  class FormValidation { Line 700  class FormValidation {
700          if ( $len > VALIDATE_MAXSIZE ) {          if ( $len > VALIDATE_MAXSIZE ) {
701              return $this->_error("INVALID_PASSWORD_LENGTH");              return $this->_error("INVALID_PASSWORD_LENGTH");
702          }          }
         if ( !$this->is_alphanum($password) ) {  
             return $this->_error("INVALID_PASSWORD_CHARACTERS");  
         }  
703          return TRUE;          return TRUE;
704      }      }
705    
# Line 721  class FormValidation { Line 741  class FormValidation {
741       * jane@blah.com or "Jane Doe <jane@blah.com>"       * jane@blah.com or "Jane Doe <jane@blah.com>"
742       *       *
743       * @param string - the value to validate       * @param string - the value to validate
744         * @param bool - allows long email name format
745       * @return TRUE = succes  FALSE = failed       * @return TRUE = succes  FALSE = failed
746       */       */
747      function is_email ($email) {      function is_email ($email, $allow_name = true) {
748    
749          //no quotes allowed          //no quotes allowed
750          if ( strstr($email, '"') || strstr($email, "'") ) {          if ( strstr($email, '"') || strstr($email, "'") ) {
# Line 734  class FormValidation { Line 755  class FormValidation {
755          //"Foo Bar <foo@bar.com>"          //"Foo Bar <foo@bar.com>"
756          $name = explode(" ", $email);          $name = explode(" ", $email);
757          if ( count($name) > 1 ) {          if ( count($name) > 1 ) {
758    
759                if (!$allow_name) {
760                    return $this->_error("INVALID_EMAIL");
761                }
762    
763              //it looks like they gave us an email              //it looks like they gave us an email
764              //with a leading name such as Foo Bar <foo@bar.com>              //with a leading name such as Foo Bar <foo@bar.com>
765              //find the email address inside <>              //find the email address inside <>
# Line 762  class FormValidation { Line 788  class FormValidation {
788          }          }
789    
790          // "@.", ".@.", "_@.", .com, .net, .edu, .blah          // "@.", ".@.", "_@.", .com, .net, .edu, .blah
791          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) ) {
792              return $this->_error("INVALID_EMAIL");              return $this->_error("INVALID_EMAIL");
793          }          }
794    
# Line 787  class FormValidation { Line 813  class FormValidation {
813       * This function tests a string that may       * This function tests a string that may
814       * contain many email addresses seperated by       * contain many email addresses seperated by
815       * commas       * commas
816       *       *
817       * @param string - the value to validate       * @param string - the value to validate
818       * @return TRUE = succes  FALSE = failed       * @return TRUE = succes  FALSE = failed
819       */       */
# Line 796  class FormValidation { Line 822  class FormValidation {
822    
823          $email_arr = explode(",", $emails);          $email_arr = explode(",", $emails);
824          foreach( $email_arr as $key => $email) {          foreach( $email_arr as $key => $email) {
825              $res = $this->is_email($email);              $res = $this->is_email(trim($email));
826              if ( $res !== TRUE ) {              if ( $res !== TRUE ) {
827                  return $res;                  return $res;
828              }              }
# Line 806  class FormValidation { Line 832  class FormValidation {
832    
833    
834      /**      /**
835       * This method validates a string as a       * This method validates a string as a
836       * leap year.       * leap year.
837       *       *
838       * @param string - the value to validate       * @param string - the value to validate
# Line 900  class FormValidation { Line 926  class FormValidation {
926    
927    
928      /**      /**
929       * This validates an array of values as a       * This validates an array of values as a
930       * valid date time       * valid date time
931       *       *
932       * NOTE: array must contain       * NOTE: array must contain
933       *       array(       *       array(
934       *             "month" => VALUE,       *             "month" => VALUE,
935       *             "day" => VALUE,       *             "day" => VALUE,
936       *             "year" => VALUE,       *             "year" => VALUE,
937       *             "hour" => VALUE,       *             "hour" => VALUE,
938       *             "minutes" => VALUE,       *             "minutes" => VALUE,
939       *             "seconds" => VALUE);       *             "seconds" => VALUE);
# Line 935  class FormValidation { Line 961  class FormValidation {
961      }      }
962    
963      /**      /**
964       * This validates an array of fields as a       * This validates an array of fields as a
965       * valid time of the day       * valid time of the day
966       *       *
967       * NOTE: array must contain       * NOTE: array must contain
# Line 1053  class FormValidation { Line 1079  class FormValidation {
1079      }      }
1080    
1081      /**      /**
1082       * This method validates a string as a valid url       * This method validates a string as a valid url
1083       * It inclues the prefix, hostname/ip, port number       * It inclues the prefix, hostname/ip, port number
1084       * and path.       * and path.
1085       *       *

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed