Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>form validator</title>
- </head>
- <body>
- <?php
- global $formData;
- $requestMethod = $_SERVER['REQUEST_METHOD'];
- class formValidator {
- public $empty;
- private $currentRules;
- public $fieldName;
- public $fieldValue;
- private $formFieldName;
- private $formFieldValue;
- private $badVars;
- private $countOfBadVars;
- private static $errorReport;
- private $errorsCount;
- function __construct(){
- $this->activateListOfBadVars();
- }
- public function acceptRules($rules){
- $this->currentRules = $rules;
- if(!empty($this->currentRules) || !empty($rules)){
- $this->verifyFields();
- } else {
- $this->prepareErrorReport('there no rules to check fields with');
- }
- }
- public function prepareErrorReport($errorMessage, $fromCase='', $dataType=''){
- self::$errorReport[] = $errorMessage . "<br />";
- $this->errorsCount = count(self::$errorReport);
- }
- protected function activateListOfBadVars(){
- $variableList[] = 'submit_button';
- $this->countOfBadVars = count($variableList);
- $this->badVars = $variableList;
- for ($i = 0; $i < ($this->countOfBadVars); $i++){
- unset($_POST[$this->badVars[$i]]);
- }
- }
- private function trimmer($field, $value){
- $this->formFieldName[] = $this->fieldName = strip_tags(trim($field));
- $this->formFieldValue[] = $this->fieldValue = strip_tags(trim($value));
- }
- private function inputChecker($pattern, $dataType, $errorMessage){
- if (!empty($this->fieldValue)) {
- $currentCountOfErrors = 0;
- foreach ($pattern as $currentPattern) {
- if (substr_count(($this->fieldValue), $currentPattern) > 0) {
- ++$currentCountOfErrors;
- }
- }
- if ($currentCountOfErrors > 0) {
- $this->prepareErrorReport($errorMessage . " in $dataType field ", 'onlychars', $dataType);
- }
- }
- }
- private function verifyFields(){
- foreach ($_POST as $POSTfieldName => $POSTfieldValue) {
- $this->trimmer($POSTfieldName, $POSTfieldValue);
- $dataType = str_replace('_field', '', $this->fieldName);
- if (array_key_exists($dataType, $this->currentRules)) {
- foreach($this->currentRules[$dataType] as $currentRule => $errorMessage) {
- switch($currentRule){
- case 'required':
- if (empty($this->fieldValue)) {
- $this->prepareErrorReport($errorMessage, 'required', $dataType);
- }
- break;
- case 'maxlength':
- if (strlen($this->fieldValue) > 32) {
- $this->prepareErrorReport($errorMessage, 'maxlength', $dataType);
- }
- break;
- case 'onlychars':
- $pattern = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',', ".");
- $this->inputChecker($pattern, $dataType, $errorMessage);
- break;
- case 'onlynumbers':
- $pattern = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',', ".");
- $this->inputChecker($pattern, $dataType, $errorMessage);
- break;
- case 'specchar':
- $pattern = array('!', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '|', '?', '±', '~', '`', "'", '[', ']', ';', '\\', '/', '<', ',');
- if (!empty($this->fieldValue)) {
- $currentCountOfErrors = 0;
- foreach ($pattern as $currentPattern) {
- if ((substr_count(($this->fieldValue), $currentPattern) > 0) || (substr_count(($this->fieldValue), '@') > 1) || (substr_count(($this->fieldValue), '@') <= 0) || (substr_count(($this->fieldValue), '.') < 1)) {
- ++$currentCountOfErrors;
- }
- }
- if ($currentCountOfErrors > 0) {
- $this->prepareErrorReport($errorMessage . " in $dataType field ", 'onlychars', $dataType);
- }
- }
- break;
- }
- }
- }
- }
- if(!in_array($_POST['sex_listbox'], array('male', 'female', 'select your sex'))) {
- $this->prepareErrorReport('there something wrong with your sex');
- }
- if (in_array($_POST['sex_listbox'], array('select your sex'))){
- $this->prepareErrorReport('select your sex');
- }
- }
- public function displayAllErrors(){
- for ($i = 0; $i < ($this->errorsCount); $i++){
- echo self::$errorReport[$i];
- }
- }
- public function takeOutSomeVars(){
- global $formData;
- $formKey = $this->formFieldName;
- $formValue = $this->formFieldValue;
- $formData = array_combine($formKey, $formValue);
- }
- }
- if ($requestMethod == 'POST') {
- $emptyRules = '';
- $rules = array(
- 'name' => array(
- 'required' => 'name is required field',
- 'maxlength' => 'max length must be 32',
- 'onlychars' => 'only characters [A-Z][a-z] are available to input'
- ),
- 'surname' => array(
- 'required' => 'surname is required field',
- 'maxlength' => 'max length must be 32',
- 'onlychars' => 'only characters [A-Z][a-z] are available to input'
- ),
- 'email' => array(
- 'required' => 'email is required field',
- 'maxlength' => 'max length must be 32',
- 'specchar' => 'there problems with your entered email (be sure you entered it like: info@site.com)'
- ),
- 'telephone' => array(
- 'required' => 'telephone is required field',
- 'maxlength' => 'max length must be 12',
- 'onlynumbers' => 'only [+, -] sign and numbers [0-9] are available to input'
- )
- );
- $newValidator = new formValidator();
- $newValidator->acceptRules($rules);
- $newValidator->displayAllErrors();
- $newValidator->takeOutSomeVars();
- }
- #EMPTY
- ?>
- <form action="" method="POST" name="validator_form">
- <table name="validator_table">
- <tr>
- <td>
- <input type="text" name="name_field" maxlength="32" value="<?php echo !empty($formData['name_field']) ? $formData['name_field'] : ''; ?>" />
- </td>
- <td>
- : Name field
- </td>
- </tr>
- <tr>
- <td>
- <input type="text" name="surname_field" maxlength="32" value="<?php echo !empty($formData['surname_field']) ? $formData['surname_field'] : ''; ?>" />
- <td>
- : Surname field
- </td>
- </tr>
- <tr>
- <td>
- <input type="text" name="email_field" maxlength="32" value="<?php echo !empty($formData['email_field']) ? $formData['email_field'] : ''; ?>" />
- </td>
- <td>
- : E-mail field
- </td>
- </tr>
- <tr>
- <td>
- <input type="text" name="telephone_field" maxlength="12" value="<?php echo !empty($formData['telephone_field']) ? $formData['telephone_field'] : ''; ?>" />
- </td>
- <td>
- : Telephone field
- </td>
- </tr>
- <tr>
- <td>
- <select name="sex_listbox">
- <option value="select your sex" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'select your sex' ? 'selected' : '' ?>>select your sex</option>
- <option value="male" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'male' ? 'selected' : '' ?>>male</option>
- <option value="female" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'female' ? 'selected' : '' ?>>female</option>
- <option value="ufo" <?php echo isset($formData['sex_listbox']) && $formData['sex_listbox'] == 'ufo' ? 'selected' : '' ?>>ufo</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>
- <input type="submit" name="submit_button" value="submit" />
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- <?php
- # EMPTY
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement