Andrew's Web Libraries (AWL)
Validation.php
1<?php
11require_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">
70function $this->func_name(form)
71{
72 var error_message = "";\n
73EOHTML;
74
75 foreach($this->rules as $rule) {
76 list($fieldname, $error_message, $function_name) = $rule;
77
78 $html .= <<<EOHTML
79if(!$function_name(form.$prefix$fieldname)) error_message += "$error_message\\n";
80EOHTML;
81 }
82
83 $html .= <<<EOHTML
84if(error_message == "") return true;
85alert("Errors:"+"\\n"+error_message);
86return false;
87}
88</script>
89EOHTML;
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
RenderJavascript($prefix="")
Definition: Validation.php:64
valid_date_format($field_string)
Definition: Validation.php:193
positive_dollars($field_string)
Definition: Validation.php:150
valid_email_format($field_string)
Definition: Validation.php:179
AddRule( $fieldname, $error_message, $function_name)
Definition: Validation.php:53
Validate($object)
Definition: Validation.php:99
not_empty($field_string)
Definition: Validation.php:129
__construct($func_name)
Definition: Validation.php:40
selected($field_string)
Definition: Validation.php:139
positive_integer($field_string)
Definition: Validation.php:167