PageRenderTime 114ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/libraries/formvalidator/formvalidator.php

http://joostina.googlecode.com/
PHP | 505 lines | 408 code | 64 blank | 33 comment | 74 complexity | 3e856441e788c43d168e2def0cd12a2a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?PHP
  2. /*
  3. -------------------------------------------------------------------------
  4. PHP Form Validator (formvalidator.php)
  5. Version 1.0
  6. Copyright (C) 2008 html-form-guide.com. All rights reserved.
  7. You can freely use this script.
  8. You may adapt this script for your own needs, provided these opening credit
  9. lines are kept intact.
  10. This Form validation script is distributed free from html-form-guide.com
  11. For updates, please visit:
  12. http://www.html-form-guide.com/php-form/php-form-validation.php
  13. Questions & comments please send to support@html-form-guide.com
  14. -------------------------------------------------------------------------
  15. */
  16. /**
  17. * Carries information about each of the form validations
  18. */
  19. class ValidatorObj {
  20. var $variable_name;
  21. var $validator_string;
  22. var $error_string;
  23. }
  24. /**
  25. * Base class for custom validation objects
  26. **/
  27. class CustomValidator {
  28. function DoValidate(&$formars,&$error_hash) {
  29. return true;
  30. }
  31. }
  32. /** Default error messages*/
  33. define("E_VAL_REQUIRED_VALUE","?? ??????? %s");
  34. define("E_VAL_MAXLEN_EXCEEDED","???????????? ????? ?? ?????? ????????? %s.");
  35. define("E_VAL_MINLEN_CHECK_FAILED","???????? ?????? ???? ?????? %d ??? %s");
  36. define("E_VAL_ALNUM_CHECK_FAILED","????????? ?????? ????? %s");
  37. define("E_VAL_ALNUM_S_CHECK_FAILED","???????? ?????? ????? ? ????? %s");
  38. define("E_VAL_NUM_CHECK_FAILED","????????? ?????? ????? %s");
  39. define("E_VAL_ALPHA_CHECK_FAILED","????????? ?????? ??????? %s");
  40. define("E_VAL_ALPHA_S_CHECK_FAILED","????????? ?????? ???????, ?? %s");
  41. define("E_VAL_EMAIL_CHECK_FAILED","????? ???????????? ? ????? ?? ?????");
  42. define("E_VAL_LESSTHAN_CHECK_FAILED","???????? ?????? ???? ????? %f ??? %s");
  43. define("E_VAL_GREATERTHAN_CHECK_FAILED","???????? ?????? ???? ?????? ??? %f ??? %s");
  44. define("E_VAL_REGEXP_CHECK_FAILED","??? ???-?? ?? ???%s");
  45. define("E_VAL_DONTSEL_CHECK_FAILED","????????? ??????? ?? ????? %s");
  46. define("E_VAL_SELMIN_CHECK_FAILED","???????? ??????????? %d ??????? ??? %s");
  47. define("E_VAL_SELONE_CHECK_FAILED","???????? ??????? %s");
  48. define("E_VAL_EQELMNT_CHECK_FAILED","???????? %s ?????? ????????? ? %s");
  49. define("E_VAL_NEELMNT_CHECK_FAILED","???????? %s ?? ?????? ???? ????? ?? ??? %s");
  50. /**
  51. * FormValidator: The main class that does all the form validations
  52. **/
  53. class FormValidator {
  54. var $validator_array;
  55. var $error_hash;
  56. var $custom_validators;
  57. var $js_validator;
  58. function get_js_validator($task) {
  59. return json_encode($this->js_validator[$task]);
  60. // return $v = $this->js_validator;
  61. // return json_encode($this->js_validator);
  62. }
  63. function FormValidator() {
  64. $this->validator_array = array();
  65. $this->error_hash = array();
  66. $this->custom_validators=array();
  67. }
  68. function AddCustomValidator(&$customv) {
  69. array_push($this->custom_validators,$customv);
  70. }
  71. function addValidation($variable,$validator,$error) {
  72. $validator_obj = new ValidatorObj();
  73. $validator_obj->variable_name = $variable;
  74. $validator_obj->validator_string = $validator;
  75. $validator_obj->error_string = $error;
  76. array_push($this->validator_array,$validator_obj);
  77. }
  78. function GetErrors() {
  79. return $this->error_hash;
  80. }
  81. function ValidateForm() {
  82. $bret = true;
  83. $error_string="";
  84. $error_to_display = "";
  85. if(strcmp($_SERVER['REQUEST_METHOD'],'POST')==0) {
  86. $form_variables = $_POST;
  87. }
  88. else {
  89. $form_variables = $_GET;
  90. }
  91. $vcount = count($this->validator_array);
  92. foreach($this->validator_array as $val_obj) {
  93. if(!$this->ValidateObject($val_obj,$form_variables,$error_string)) {
  94. $bret = false;
  95. $this->error_hash[$val_obj->variable_name] = $error_string;
  96. }
  97. }
  98. if(true == $bret && count($this->custom_validators) > 0) {
  99. foreach( $this->custom_validators as $custom_val) {
  100. if(false == $custom_val->DoValidate($form_variables,$this->error_hash)) {
  101. $bret = false;
  102. }
  103. }
  104. }
  105. return $bret;
  106. }
  107. function ValidateObject($validatorobj,$formvariables,&$error_string) {
  108. $bret = true;
  109. $splitted = explode("=",$validatorobj->validator_string);
  110. $command = $splitted[0];
  111. $command_value = '';
  112. if(isset($splitted[1]) && strlen($splitted[1])>0) {
  113. $command_value = $splitted[1];
  114. }
  115. $default_error_message="";
  116. $input_value ="";
  117. if(isset($formvariables[$validatorobj->variable_name])) {
  118. $input_value = $formvariables[$validatorobj->variable_name];
  119. }
  120. $bret = $this->ValidateCommand($command,$command_value,$input_value,
  121. $default_error_message,
  122. $validatorobj->variable_name,
  123. $formvariables);
  124. if(false == $bret) {
  125. if(isset($validatorobj->error_string) &&
  126. strlen($validatorobj->error_string)>0) {
  127. $error_string = $validatorobj->error_string;
  128. }
  129. else {
  130. $error_string = $default_error_message;
  131. }
  132. }//if
  133. return $bret;
  134. }
  135. function validate_req($input_value, &$default_error_message,$variable_name) {
  136. $bret = true;
  137. if(!isset($input_value) ||
  138. strlen($input_value) <=0) {
  139. $bret=false;
  140. $default_error_message = sprintf(E_VAL_REQUIRED_VALUE,$variable_name);
  141. }
  142. return $bret;
  143. }
  144. function validate_maxlen($input_value,$max_len,$variable_name,&$default_error_message) {
  145. $bret = true;
  146. if(isset($input_value) ) {
  147. $input_length = strlen($input_value);
  148. if($input_length > $max_len) {
  149. $bret=false;
  150. $default_error_message = sprintf(E_VAL_MAXLEN_EXCEEDED,$variable_name);
  151. }
  152. }
  153. return $bret;
  154. }
  155. function validate_minlen($input_value,$min_len,$variable_name,&$default_error_message) {
  156. $bret = true;
  157. if(isset($input_value) ) {
  158. $input_length = strlen($input_value);
  159. if($input_length < $min_len) {
  160. $bret=false;
  161. $default_error_message = sprintf(E_VAL_MINLEN_CHECK_FAILED,$min_len,$variable_name);
  162. }
  163. }
  164. return $bret;
  165. }
  166. function test_datatype($input_value,$reg_exp) {
  167. if(ereg($reg_exp,$input_value)) {
  168. return false;
  169. }
  170. return true;
  171. }
  172. function validate_email($email) {
  173. return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
  174. }
  175. function validate_for_numeric_input($input_value,&$validation_success) {
  176. $more_validations=true;
  177. $validation_success = true;
  178. if(strlen($input_value)>0) {
  179. if(false == is_numeric($input_value)) {
  180. $validation_success = false;
  181. $more_validations=false;
  182. }
  183. }
  184. else {
  185. $more_validations=false;
  186. }
  187. return $more_validations;
  188. }
  189. function validate_lessthan($command_value,$input_value,
  190. $variable_name,&$default_error_message) {
  191. $bret = true;
  192. if(false == $this->validate_for_numeric_input($input_value,
  193. $bret)) {
  194. return $bret;
  195. }
  196. if($bret) {
  197. $lessthan = doubleval($command_value);
  198. $float_inputval = doubleval($input_value);
  199. if($float_inputval >= $lessthan) {
  200. $default_error_message = sprintf(E_VAL_LESSTHAN_CHECK_FAILED,
  201. $lessthan,
  202. $variable_name);
  203. $bret = false;
  204. }//if
  205. }
  206. return $bret ;
  207. }
  208. function validate_greaterthan($command_value,$input_value,$variable_name,&$default_error_message) {
  209. $bret = true;
  210. if(false == $this->validate_for_numeric_input($input_value,$bret)) {
  211. return $bret;
  212. }
  213. if($bret) {
  214. $greaterthan = doubleval($command_value);
  215. $float_inputval = doubleval($input_value);
  216. if($float_inputval <= $greaterthan) {
  217. $default_error_message = sprintf(E_VAL_GREATERTHAN_CHECK_FAILED,
  218. $greaterthan,
  219. $variable_name);
  220. $bret = false;
  221. }//if
  222. }
  223. return $bret ;
  224. }
  225. function validate_select($input_value,$command_value,&$default_error_message,$variable_name) {
  226. $bret=false;
  227. if(is_array($input_value)) {
  228. foreach($input_value as $value) {
  229. if($value == $command_value) {
  230. $bret=true;
  231. break;
  232. }
  233. }
  234. }
  235. else {
  236. if($command_value == $input_value) {
  237. $bret=true;
  238. }
  239. }
  240. if(false == $bret) {
  241. $default_error_message = sprintf(E_VAL_SHOULD_SEL_CHECK_FAILED,
  242. $command_value,$variable_name);
  243. }
  244. return $bret;
  245. }
  246. function validate_dontselect($input_value,$command_value,&$default_error_message,$variable_name) {
  247. $bret=true;
  248. if(is_array($input_value)) {
  249. foreach($input_value as $value) {
  250. if($value == $command_value) {
  251. $bret=false;
  252. $default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
  253. break;
  254. }
  255. }
  256. }
  257. else {
  258. if($command_value == $input_value) {
  259. $bret=false;
  260. $default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
  261. }
  262. }
  263. return $bret;
  264. }
  265. function ValidateCommand($command,$command_value,$input_value,&$default_error_message,$variable_name,$formvariables) {
  266. $bret=true;
  267. switch($command) {
  268. case 'req': {
  269. $bret = $this->validate_req($input_value, $default_error_message, $variable_name);
  270. $this->js_validator['rules'][$variable_name]['required'] = true;
  271. $this->js_validator['messages'][$variable_name]['required'] = $default_error_message;
  272. break;
  273. }
  274. case 'maxlen': {
  275. $max_len = intval($command_value);
  276. $bret = $this->validate_maxlen($input_value,$max_len,$variable_name,
  277. $default_error_message);
  278. $this->js_validator['rules'][$variable_name]['maxlength'] = $max_len;
  279. $this->js_validator['messages'][$variable_name]['maxlength'] = $default_error_message;
  280. break;
  281. }
  282. case 'minlen': {
  283. $min_len = intval($command_value);
  284. $bret = $this->validate_minlen($input_value,$min_len,$variable_name,
  285. $default_error_message);
  286. $this->js_validator['rules'][$variable_name]['minlength'] = $min_len;
  287. $this->js_validator['messages'][$variable_name]['minlength'] = $default_error_message;
  288. break;
  289. }
  290. case 'alnum': {
  291. $bret= $this->test_datatype($input_value,"[^A-Za-z0-9]");
  292. if(false == $bret) {
  293. $default_error_message = sprintf(E_VAL_ALNUM_CHECK_FAILED,$variable_name);
  294. }
  295. break;
  296. }
  297. case 'alnum_s': {
  298. $bret= $this->test_datatype($input_value,"[^A-Za-z0-9 ]");
  299. if(false == $bret) {
  300. $default_error_message = sprintf(E_VAL_ALNUM_S_CHECK_FAILED,$variable_name);
  301. }
  302. break;
  303. }
  304. case 'num':
  305. case 'numeric': {
  306. $bret= $this->test_datatype($input_value,"[^0-9]");
  307. if(false == $bret) {
  308. $default_error_message = sprintf(E_VAL_NUM_CHECK_FAILED,$variable_name);
  309. }
  310. $this->js_validator['rules'][$variable_name]['number'] = true;
  311. $this->js_validator['messages'][$variable_name]['number'] = $default_error_message;
  312. break;
  313. }
  314. case 'alpha': {
  315. $bret= $this->test_datatype($input_value,"[^A-Za-z]");
  316. if(false == $bret) {
  317. $default_error_message = sprintf(E_VAL_ALPHA_CHECK_FAILED,$variable_name);
  318. }
  319. break;
  320. }
  321. case 'alpha_s': {
  322. $bret= $this->test_datatype($input_value,"[^A-Za-z ]");
  323. if(false == $bret) {
  324. $default_error_message = sprintf(E_VAL_ALPHA_S_CHECK_FAILED,$variable_name);
  325. }
  326. break;
  327. }
  328. case 'email': {
  329. if(isset($input_value) && strlen($input_value)>0) {
  330. $bret= $this->validate_email($input_value);
  331. if(false == $bret) {
  332. $default_error_message = E_VAL_EMAIL_CHECK_FAILED;
  333. }
  334. }
  335. $this->js_validator['rules'][$variable_name]['email'] = true;
  336. $this->js_validator['messages'][$variable_name]['email'] = E_VAL_EMAIL_CHECK_FAILED;
  337. break;
  338. }
  339. case "lt":
  340. case "lessthan": {
  341. $bret = $this->validate_lessthan($command_value,
  342. $input_value,
  343. $variable_name,
  344. $default_error_message);
  345. $this->js_validator['rules'][$variable_name]['max'] = $command_value;
  346. $this->js_validator['messages'][$variable_name]['max'] = $default_error_message;
  347. break;
  348. }
  349. case "gt":
  350. case "greaterthan": {
  351. $bret = $this->validate_greaterthan($command_value,
  352. $input_value,
  353. $variable_name,
  354. $default_error_message);
  355. $this->js_validator['rules'][$variable_name]['min'] = $command_value;
  356. $this->js_validator['messages'][$variable_name]['min'] = $default_error_message;
  357. break;
  358. }
  359. case "regexp": {
  360. if(isset($input_value) && strlen($input_value)>0) {
  361. if(!preg_match("$command_value",$input_value)) {
  362. $bret=false;
  363. $default_error_message = sprintf(E_VAL_REGEXP_CHECK_FAILED,$variable_name);
  364. }
  365. }
  366. break;
  367. }
  368. case "dontselect":
  369. case "dontselectchk":
  370. case "dontselectradio": {
  371. $bret = $this->validate_dontselect($input_value,
  372. $command_value,
  373. $default_error_message,
  374. $variable_name);
  375. break;
  376. }//case
  377. case "shouldselchk":
  378. case "selectradio": {
  379. $bret = $this->validate_select($input_value,
  380. $command_value,
  381. $default_error_message,
  382. $variable_name);
  383. break;
  384. }//case
  385. case "selmin": {
  386. $min_count = intval($command_value);
  387. if(isset($input_value)) {
  388. if($min_count > 1) {
  389. $bret = (count($input_value) >= $min_count )?true:false;
  390. }
  391. else {
  392. $bret = true;
  393. }
  394. }
  395. else {
  396. $bret= false;
  397. $default_error_message = sprintf(E_VAL_SELMIN_CHECK_FAILED,$min_count,$variable_name);
  398. }
  399. break;
  400. }//case
  401. case "selone": {
  402. if(false == isset($input_value)||
  403. strlen($input_value)<=0) {
  404. $bret= false;
  405. $default_error_message = sprintf(E_VAL_SELONE_CHECK_FAILED,$variable_name);
  406. }
  407. break;
  408. }
  409. case "eqelmnt": {
  410. if(isset($formvariables[$command_value]) &&
  411. strcmp($input_value,$formvariables[$command_value])==0 ) {
  412. $bret=true;
  413. }
  414. else {
  415. $bret= false;
  416. $default_error_message = sprintf(E_VAL_EQELMNT_CHECK_FAILED,$variable_name,$command_value);
  417. }
  418. // $this->js_validator['rules'][$variable_name]['equalTo'] = '.' . $command_value;
  419. // $this->js_validator['messages'][$variable_name]['equalTo'] = $default_error_message;
  420. break;
  421. }
  422. case "neelmnt": {
  423. if(isset($formvariables[$command_value]) &&
  424. strcmp($input_value,$formvariables[$command_value]) !=0 ) {
  425. $bret=true;
  426. }
  427. else {
  428. $bret= false;
  429. $default_error_message = sprintf(E_VAL_NEELMNT_CHECK_FAILED,$variable_name,$command_value);
  430. }
  431. break;
  432. }
  433. }//switch
  434. return $bret;
  435. }//validdate command
  436. }
  437. /*
  438. Copyright (C) 2008 html-form-guide.com . All rights reserved.
  439. */