php tutorial
validation php using library with bootstrap in oop
overview:
Hello 😊 , today we will introduce something very important in php oop,
which is the topic of checking inputs, so today we will explain about
this topic in a very simple way using a library that I developed before
files structure:
which is the topic of checking inputs, so today we will explain about
this topic in a very simple way using a library that I developed before
files structure:
Step1 :We put the validator library code in this file.
validator.class.php:
<?php/* #### class by skmcoder */ class validator { /* find_errors prop */ private $errors = []; /* find_errors prop */ /* validation prop */ private $input_names = array(); /* validation prop */ /* check all validation */ private $check = array(); /* check all validation */ /* masgs */ private $masgs=array(); public $Messages = [ "Date-error" => "Please enter a valid date in the field", "Time-error" => "Please type a valid time representing 24 hours in the field", "Number-word-max-error" => "The number of words or numbers must be in a field", "Number-word-min-error" => "You must be the number of words or numbers in a field", "Number-error" => "Please enter numbers only in the field", "url-error" => "Please enter a valid link in the field", "eng-error" => "Please type English only in the field", "email-error" => "Please type a valid email in the field" ]; /* masgs */ /* flag all validation */ // public $ok; /* custom name */ private $custom_name = array(); /* custom name */ private $custom_name2 = array(); /* flag all validation */ public function test_validate() { return $this->find_errors($this->check); } public function __construct($valid_array) { foreach ($valid_array as $key => $value) { array_push($this->input_names, strip_tags(htmlentities(trim($_POST[$key])))); foreach ($value as $option) { /* start validation part */ switch ($option) { case substr($option, 0, 3) == "max": $this->max(substr($option, 4), $key); break; case substr($option, 0, 3) == "min": $this->min(substr($option, 4), $key); break; case substr($option, 0, 4) == "name": $this->custom_name[] = $key . ':' . substr($option, 5); break; case "require": $this->require($key); break; case "url": $this->url($key); break; case "number": $this->number($key); break; case "email": $this->email($key); break; case "eng": $this->eng($key); break; case "date": $this->date($key); break; case "time24": $this->time24($key); break; }; } } /*get the name inputs */ // foreach($this->input_names as $inputs){ // echo $inputs .'<br>'; // } /*get the name inputs */ } private function date($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' ' . $this->Messages['Date-error'] . $custom_name_arabic); } else { array_push($this->masgs, ' ' . $this->Messages['Date-error'] . $input_name); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function time24($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } if (!preg_match("#^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#", $input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' ' . $this->Messages['Time-error'] . $custom_name_arabic); } else { array_push($this->masgs, ' ' . $this->Messages['Time-error'] . $input_name); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function max($max_number, $input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim(@$_POST[$input_name]))); } // echo strlen($input_value) .'<Br>'; if (strlen("$input_value") > $max_number) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' ' . ' ' . ' ' . $this->Messages['Number-word-max-error'] . ' ' . $custom_name_arabic . "don't go beyond " . ' ' . $max_number); } else { array_push($this->masgs, $max_number . ' ' . "don't go beyond " . ' ' . $input_name . ' ' . $this->Messages['Number-word-max-error']); } array_push($this->check, "false"); } else { array_push($this->check, "true"); }; } private function min($min_number, $input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim($_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } // echo strlen($input_value) .'<Br>'; if (strlen("$input_value") < $min_number) { if (isset($custom_name_arabic)) { array_push($this->masgs, $min_number . ' ' . ' ' . ' ' . $this->Messages["Number-word-min-error"] . ' ' . $custom_name_arabic . ' at least '); } else { array_push($this->masgs, ' ' . ' at least ' . ' ' . $input_name . ' ' . $this->Messages["Number-word-min-error"] . $min_number); } array_push($this->check, "false"); } else { array_push($this->check, "true"); }; } private function number($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim(@$_POST[$input_name]))); } if (!preg_match("/^[0-9]+$/", $input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' ' . $this->Messages['Number-error'] . $custom_name_arabic); } else { array_push($this->masgs, $input_name . ' ' . $this->Messages['Number-error']); }; array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function require($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim(@$_POST[$input_name]))); } if (empty($input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' Do not leave a field ' . $custom_name_arabic . ' ' . 'empty'); } else { array_push($this->masgs, 'empty' . ' ' . $input_name . ' ' . ' Do not leave a field'); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function url($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } if (!filter_var($input_value, FILTER_VALIDATE_URL)) { if (isset($custom_name_arabic)) { array_push($this->masgs, ' ' . $this->Messages['url-error'] . $custom_name_arabic); } else { array_push($this->masgs, $input_name . ' ' . $this->Messages['url-error']); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function eng($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } $regex = '/^[a-zA-Z0-9$@$!%*?&#^-_. +]+$/'; if (!preg_match($regex, $input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, $this->Messages['eng-error'] . ' ' . $custom_name_arabic); } else { array_push($this->masgs, $input_name . ' ' . $this->Messages['eng-error']); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } private function email($input_name = null, $custom_name_arabic = null) { foreach ($this->custom_name as $value) { $pos = strpos($value, ":"); /*custom name */ $name = substr($value, 0, $pos); /* origanl name */ $custom_name = substr($value, $pos + 1); if ($name == $input_name) { $custom_name_arabic = $custom_name; } } if (in_array(strip_tags(htmlentities(trim(@$_POST[$input_name]))), $this->input_names)) { $input_value = strip_tags(htmlentities(trim($_POST[$input_name]))); } $regex = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/'; if (!preg_match($regex, $input_value)) { if (isset($custom_name_arabic)) { array_push($this->masgs, $this->Messages['email-error'] . ' ' . $custom_name_arabic); } else { array_push($this->masgs, $input_name . ' ' . $this->Messages['email-error']); } array_push($this->check, "false"); } else { array_push($this->check, "true"); } } public function errors_validate() { foreach ($this->masgs as $errors) { echo ' <div class="alert alert-dismissible alert-warning text-right direction-rtl" role="alert"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>' . $errors . '</strong>. </div>'; } } public function find_errors($errors_array) { $error = $this->errors[] = $errors_array; $flag_false = array(); $flag_true = array(); $errors_array = array(); for ($i = 0; $i < count($error); $i++) { if ($error[$i] == "false") { array_push($flag_false, "false"); } else { array_push($flag_true, "true"); } } if ($flag_false != null) { return false; } else if ($flag_true != null) { return true; } // return $this->chack_bool; } };
use the validator library:
<?php
// start validation check
/*
Library by skmcoder
use more validation options of Library
1- max for example max:20
2- min for example min:10
3- require
4- url
5- number
6- email
7- eng
8- date
9- time24
*/
include 'validator.class.php';
if (isset($_POST['check'])) {
$validator = new validator(
[
"require" => ["require"],
"number" => ["number", "min:10"],
"english" => ["english", 'max:8' ],
"time24" => ["time24"],
"url" => ["url"],
"email" => ["email"]
],
);
if ($validator->test_validate() == true) {
//if not find errors
//write your code here /
echo '<h1 style="color:green"> Input verified successfully, no errors
</h1> ';
} else {
//if find errors
$validator->errors_validate();
}
}
?>
Step2:We put the main code on this page.
index.php:
<html lang="en">
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.72.0">
<title>validation php </title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js" integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
</head>
<body style="background-color:#f8f9fa;">
<br>
<h1 style="font-weight:bold;font-family:'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif" align="center"> validation using php oop library with bootstrap by skmcoder </h1>
<Div class="container ">
<form action="index.php" method="post">
<?php
// start validation check
/*
Library by skmcoder
use more validation options of Library
1- max for example max:20
2- min for example min:10
3- require
4- url
5- number
6- email
7- eng
8- date
9- time24
*/
include 'validator.class.php';
if (isset($_POST['check'])) {
$validator = new validator(
[
"require" => ["require"],
"number" => ["number", "min:10"],
"english" => ["english", 'max:8' ],
"time24" => ["time24"],
"url" => ["url"],
"email" => ["email"]
],
);
if ($validator->test_validate() == true) {
//if not find errors
//write your code here /
echo '<h1 style="color:green"> Input verified successfully, no errors
</h1> ';
} else {
//if find errors
$validator->errors_validate();
}
}
?>
<div class="form-row">
<br>
<div class="form-group col-md- ">
<input type="text" class="form-control" autocomplete="off" name="require" placeholder="require ">
</div>
<br>
</div>
<div class="form-group">
<input type="text" class="form-control" autocomplete="off" name="number" placeholder="number and min 10 char check ">
</div>
<br>
<div class="form-group">
<input type="text" class="form-control" autocomplete="off" name="english" placeholder="english and max 8 char check ">
</div>
<br>
<div class="form-group">
<input type="text" class="form-control" autocomplete="off" name="time24" placeholder="time24 check ">
</div>
<br>
<div class="form-group">
<input type="text" class="form-control" autocomplete="off" name="url" placeholder="url check ">
</div>
<br>
<div class="form-row">
<div class="form-group col-md- ">
<input type="text" class="form-control" autocomplete="off" name="email" placeholder="email check ">
</div>
<br><br>
<h3 align="center"> <button type="submit" name="check" class="btn btn-info">check</button>
</h3>
</form>
</Div>
</body>
</html>
