validEX | Simple PHP Form Validator

function validEX($field, $rules) { $rules = explode("|", $rules); foreach ($rules as $key => $rule) { preg_match_all('/(\w+)[^,\]]?/', $rule, $rule); switch ($rule[1][0]) { // REQUIRED case 'required': if ($field == null || empty($field)) { return [false, 'This field is required!']; } break; // NO WHITE SPACES case 'nospace': if (preg_match('/[ ]/', $field)) { return [false, '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]) { return [false, "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]) { return [false, "This field must contain exactly {$rule[1][1]} characters. ".strlen($field)." given"]; } } break; // ALPHA-NUMERIC CHARACTERS ONLY case 'alphanum': if (preg_match('/\W /u', $field)) { return [false, "Invalid characters found! Only alpha-numeric characters allowed."]; } break; // ALPHA CHARACTERS ONLY case 'alpha': if (preg_match('/[^\p{Latin} ]/u', $field)) { return [false, "Invalid characters found! Only alphabetic characters allowed."]; } break; // SPECIAL CHARACTERS ALLOWED case 'special': if (preg_match('/[^\p{Latin}0-9_\-!@#$%&*+~]/iu', $field)) { return [false, 'Invalid characters found! A few special characters are allowed! (_-!@#$%&*+~)']; } break; // INTEGER ONLY case 'int': if (!preg_match('/^(\-|\+)?\d+$/', $field)) { return [false, 'This field must contain an integer number.']; } break; // REAL NUMBER case 'real': if (!preg_match('/^(\-|\+)?\d+(.[\d]+)?$/', $field)) { return [false, 'This field must contain a real number.']; } break; // NUMBER RANGE case 'range': if ($field < $rule[1][1] || $field > $rule[1][2]) { return [false, "This field must contain a number between {$rule[1][1]} and {$rule[1][2]}."]; } break; // POSITIVE case 'pos': if ($field < 0) { return [false, "This field requires a positive number!"]; } break; // MATCH FIELD case 'match': if ($field != SUBMIT[$rule[1][1]]) { return [false, "This field doesn't match the required one."]; } break; // EMAIL FIELD case 'email': if (preg_match('/^[\w.\-]+@[\w.\-]+\.[\w.\-]+$/', $field) == 0) { return [false, "A valid email address is required!"]; } break; // SUCCESS CASE default: return true; break; } } }
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.