validEX | Simple PHP Form Validator

<?php class validEX { private $responses = []; public function add($field, $rules) { $data = (empty($_GET) ? $_POST : $_GET); $rules = explode("|", $rules); $fieldname = $field; $field = $data[$field]; foreach ($rules as $key => $rule) { preg_match_all('/(\w+)[^,\]]?/', $rule, $rule); switch ($rule[1][0]) { // REQUIRED case 'required': if ($field == null || empty($field)) { array_push($this->responses, [$fieldname, 'This field is required!']); } break; // NO WHITE SPACES case 'nospace': if (preg_match('/[ ]/', $field)) { array_push($this->responses, [$fieldname, 'No white-spaces are allowed!']); } break; // LENGTH case 'len': if (isset($rule[1][2])) { if (strlen($field) < $rule[1][1] || strlen($field) > $rule[1][2]) { array_push($this->responses, [$fieldname, "This field must contain between {$rule[1][1]} and {$rule[1][2]} characters. ".strlen($field)." given"]); } } else { if (strlen($field) < $rule[1][1] || strlen($field) > $rule[1][1]) { array_push($this->responses, [$fieldname, "This field must contain exactly {$rule[1][1]} characters. ".strlen($field)." given"]); } } break; // ALPHA-NUMERIC CHARACTERS ONLY case 'alphanum': if (preg_match('/[^\n a-z0-9ãõñâêîôûáéíóúàèìòùäëïöüç]/i', $field)) { array_push($this->responses, [$fieldname, "Invalid characters found! Only alpha-numeric characters allowed."]); } break; // ALPHA CHARACTERS ONLY case 'alpha': if (preg_match('/[^\n a-zãõñâêîôûáéíóúàèìòùäëïöüç]/i', $field)) { array_push($this->responses, [$fieldname, "Invalid characters found! Only alphabetic characters allowed."]); } break; // SPECIAL CHARACTERS ALLOWED case 'special': if (preg_match('/[^\n 0-9a-zãõñâêîôûáéíóúàèìòùäëïöüç_\-!?@#$%&.,*+]+/i', $field)) { array_push($this->responses, [$fieldname, 'Invalid characters found! A few special characters are allowed! (_-!@#$%&*+~)']); } break; // INTEGER ONLY case 'int': if (!preg_match('/^(\-|\+)?\d+$/', $field)) { array_push($this->responses, [$fieldname, 'This field must contain an integer number.']); } break; // REAL NUMBER case 'real': if (!preg_match('/^(\-|\+)?\d+(.[\d]+)?$/', $field)) { array_push($this->responses, [$fieldname, 'This field must contain a real number.']); } break; // NUMBER RANGE case 'range': if ($field < $rule[1][1] || $field > $rule[1][2]) { array_push($this->responses, [$fieldname, "This field must contain a number between {$rule[1][1]} and {$rule[1][2]}."]); } break; // POSITIVE case 'pos': if ($field < 0) { array_push($this->responses, [$fieldname, "This field requires a positive number!"]); } break; // MATCH FIELD case 'match': if ($field != SUBMIT[$rule[1][1]]) { array_push($this->responses, [$fieldname, "This field doesn't match the required one."]); } break; // EMAIL FIELD case 'email': if (preg_match('/^[\w.\-]+@[\w.\-]+\.[\w.\-]+$/', $field) == 0) { array_push($this->responses, [$fieldname, "A valid email address is required!"]); } break; } } } public function run() { $response = (count($this->responses) > 0 ? 0 : 1); return $response; } public function error() { return $this->responses[0]; } }
using:

<?php

require 'validEX.php';

// TRIGGER THE PLUGIN
$valid = new validEX();

// ADD TO BE FILTERED PARAMS (WILL CHECK $_GET AND $_POST BY ITSELF)
$valid->add("username", "required|len[3,16]|alphanum|nospace");
$valid->add("password", "required|len[6,24]|special|nospace");

// PERFORM THE VALIDATION
if ($valid->run()) {
// CASE SUCCESS: PROCCEED
echo "success!";
} else {
// CASE FAIL: RETURN THE ERROR MSG
echo $valid->error();
}

1 Response

Nice!

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.