Andrew's Web Libraries (AWL)
Validation.php
1 <?php
11 require_once("AWLUtilities.php");
12 
18 {
26  var $rules = array();
27 
32  var $func_name = "";
33 
40  function __construct($func_name)
41  {
42  $this->func_name = $func_name;
43  }
44 
45 
53  function AddRule( $fieldname, $error_message, $function_name )
54  {
55  $this->rules[] = array($fieldname, $error_message, $function_name );
56  }
57 
64  function RenderJavascript($prefix = "")
65  {
66  if(! count($this->rules) ) return "";
67 
68  $html = <<<EOHTML
69 <script language="JavaScript">
70 function $this->func_name(form)
71 {
72  var error_message = "";\n
73 EOHTML;
74 
75  foreach($this->rules as $rule) {
76  list($fieldname, $error_message, $function_name) = $rule;
77 
78  $html .= <<<EOHTML
79 if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
80 EOHTML;
81  }
82 
83  $html .= <<<EOHTML
84 if(error_message == "") return true;
85 alert("Errors:"+"\\n"+error_message);
86 return false;
87 }
88 </script>
89 EOHTML;
90 
91  return $html;
92  }
93 
99  function Validate($object)
100  {
101  global $c;
102 
103  if(! count($this->rules) ) return;
104 
105  $valid = true;
106 
107  foreach($this->rules as $rule) {
108  list($fieldname, $error_message, $function_name) = $rule;
109 
110  if (!$this->$function_name($object->Get($fieldname))) {
111  $valid = false;
112  $c->messages[] = $error_message;
113  }
114 
115  }
116 
117  return $valid;
118  }
119 
121 // VALIDATION FUNCTIONS
123 
129  function not_empty($field_string)
130  {
131  return ($field_string != "");
132  }
133 
139  function selected($field_string)
140  {
141  return (!($field_string == "" || $field_string == "0"));
142  }
143 
150  function positive_dollars($field_string)
151  {
152  if(!$field_string) return true;
153  if( preg_match('/^\$?[0-9]*\.?[0-9]?[0-9]?$/', $field_string) ) {
154  $field_string = preg_replace("/\$/", "", $field_string);
155  $field_string = preg_replace("/\./", "", $field_string);
156  if( intval($field_string) > 0 ) return true;
157  }
158  return false;
159  }
160 
167  function positive_integer($field_string)
168  {
169  if(!$field_string) return true;
170  return ( preg_match('/^[0-9]*$/', $field_string) );
171  }
172 
179  function valid_email_format($field_string)
180  {
181  if(!$field_string) return true;
182  // Anything printable, followed by between 1 & 5 valid domain components, with a TLD to finish
183  $pattern = "/^[[:print:]]+@([a-z0-9][a-z0-9-]*\.){1,5}[a-z]{2,5}$/i";
184  return (preg_match($pattern, $field_string));
185  }
186 
193  function valid_date_format($field_string)
194  {
195  global $session;
196 
197  if(!$field_string) return true;
198 
199  switch($session->date_format_type) {
200  case 'J':
201  if (!preg_match('/^([0-9]{4})[\/\-]([0-9]{1,2})[\/\-]([0-9]{1,2})$/', $field_string, $regs)) return false;
202  $day = intval($regs[3]);
203  $month = intval($regs[2]);
204  $year = intval($regs[1]);
205  break;
206 
207  case 'U':
208  if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
209  $day = intval($regs[2]);
210  $month = intval($regs[1]);
211  $year = intval($regs[3]);
212  break;
213 
214  case 'E':
215  default:
216  if (!preg_match('/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{4})$/', $field_string, $regs)) return false;
217  $day = intval($regs[1]);
218  $month = intval($regs[2]);
219  $year = intval($regs[3]);
220  }
221  return (checkdate ($month, $day, $year));
222  }
223 }
224 
Validation\__construct
__construct($func_name)
Definition: Validation.php:40
Validation\RenderJavascript
RenderJavascript($prefix="")
Definition: Validation.php:64
Validation\valid_date_format
valid_date_format($field_string)
Definition: Validation.php:193
Validation\selected
selected($field_string)
Definition: Validation.php:139
Validation\valid_email_format
valid_email_format($field_string)
Definition: Validation.php:179
Validation\AddRule
AddRule( $fieldname, $error_message, $function_name)
Definition: Validation.php:53
Validation\not_empty
not_empty($field_string)
Definition: Validation.php:129
Validation\positive_dollars
positive_dollars($field_string)
Definition: Validation.php:150
Validation\positive_integer
positive_integer($field_string)
Definition: Validation.php:167
Validation\Validate
Validate($object)
Definition: Validation.php:99
Validation
Definition: Validation.php:17