Simple and Dynamic Server-side Form Validation


/ Published in: PHP
Save to your folder(s)

A simple and dynamic server-side form validation script.

Setup:
1. Insert the field names in the $inputName array and the field descriptions in the respective array pointers in the $inputDesc array.
2. Change the $method to the respective method chosen.


Copy this code and paste it in your HTML
  1. $method = 'GET';
  2.  
  3. $count = 0;
  4. $errorArray = array();
  5.  
  6. $inputName = array('vit_name', 'vit_nickname', 'vit_test');
  7. $inputDesc = array('Name', 'Nickname', 'Test');
  8.  
  9. if($_SERVER['REQUEST_METHOD']==$method) {
  10. $name = '_'.$method;
  11. foreach($inputName as $ele) {
  12. if ( empty(${$name}[$ele]) ) { array_push($errorArray, "Please enter a ".$inputDesc[$count]."."); }
  13. else { }
  14. $count++;
  15. }
  16. if( !empty($errorArray) ) {
  17. for($i=0;$i<sizeof($errorArray);$i++) {
  18. echo $errorArray[$i]."<br />";
  19. }
  20. }
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.