1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: require_once("AWLUtilities.php");
12:
13: 14: 15: 16:
17: class Validation
18: {
19: 20: 21:
22: 23: 24: 25:
26: var $rules = array();
27:
28: 29: 30: 31:
32: var $func_name = "";
33:
34:
35:
36: 37: 38: 39:
40: function Validation($func_name)
41: {
42: $this->func_name = $func_name;
43: }
44:
45:
46: 47: 48: 49: 50: 51: 52:
53: function AddRule( $fieldname, $error_message, $function_name )
54: {
55: $this->rules[] = array($fieldname, $error_message, $function_name );
56: }
57:
58: 59: 60: 61: 62: 63:
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:
94: 95: 96: 97: 98:
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:
120:
121:
122:
123:
124: 125: 126: 127: 128:
129: function not_empty($field_string)
130: {
131: return ($field_string != "");
132: }
133:
134: 135: 136: 137: 138:
139: function selected($field_string)
140: {
141: return (!($field_string == "" || $field_string == "0"));
142: }
143:
144: 145: 146: 147: 148: 149:
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:
161: 162: 163: 164: 165: 166:
167: function positive_integer($field_string)
168: {
169: if(!$field_string) return true;
170: return ( preg_match('/^[0-9]*$/', $field_string) );
171: }
172:
173: 174: 175: 176: 177: 178:
179: function valid_email_format($field_string)
180: {
181: if(!$field_string) return true;
182:
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:
187: 188: 189: 190: 191: 192:
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:
225: