Skip to content

Instantly share code, notes, and snippets.

@Uunknownn
Last active September 29, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Uunknownn/05b314ecaa0a9af44b50c89a0b6e3891 to your computer and use it in GitHub Desktop.
Save Uunknownn/05b314ecaa0a9af44b50c89a0b6e3891 to your computer and use it in GitHub Desktop.
Example: validEX($_POST['username'], 'required|alphanum|nospace|len[4,16]');

Usage

// RETURN [0] (TRUE | FALSE)
// RETURN [1] (NULL | STRING ERROR MESSAGE)
$validate = validEX($_POST['username'], 'len[6,16]|alphanum|nospace');

// IF [0] IS NOT TRUE, EXIT THE SCRIPT AND RETURNS [1] STRING ERROR MESSAGE
if (!$validate[0]) die($validate[1]);

// OTHERWISE, PROCCEED WITH YOUR SCRIPT
<?php
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment