Login   Register  
PHP Classes
elePHPant
Icontem

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Matteo Hertel  >  MH Validator  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MH Validator
Validate multiple input text values
Author: By
Last change: add index file
Date: 4 months ago
Size: 22,628 bytes
 

Contents

Class file image Download
<?php include_once 'MH_validator.class.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="http://getbootstrap.com/assets/ico/favicon.png">

    <title>MH_Validator</title>

    <!-- Bootstrap core CSS -->
    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="http://getbootstrap.com/examples/offcanvas/offcanvas.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="../../assets/js/html5shiv.js"></script>
      <script src="../../assets/js/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
    <div class="navbar navbar-fixed-top navbar-inverse" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">MH_Validator</a>
        </div>
        <div class="collapse navbar-collapse">

        </div><!-- /.nav-collapse -->
      </div><!-- /.container -->
    </div><!-- /.navbar -->

    <div class="container">

      <div class="row row-offcanvas row-offcanvas-right">
        <div class="col-xs-12">
          <p class="pull-right visible-xs">
            <button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle nav</button>
          </p>
          <div class="jumbotron">
            <h1>MH_Validator demo</h1>
            <p>This is a demo of my class MH_Validator. You can find a complete post at <a href ="http://matteohertel.com/classes/MH_validator">my blog</a>
            <br />Comment and critique are more then welcome!</p>
            <br />
            <p class="text-primary">Important note: when notEmpty method is called the argument provided will be escaped</p>
          </div>
          <div class="row">
              <div class="col-6 col-sm-6 col-lg-6">
                <h2>The code</h2>
              <div class="well">
              Let's start with a simple string<br />
              $str = "<xmp><p></h1>Test's a string!</h1></p></xmp>";<br />
              <br />
              Then we have to create our object:<br />
              I want to use a short name so I am using a namespace<br />
              use \MHValidator as Valid;<br />
              <br />
              Our new object<br />
              $valid = new Valid();<br />
              <br />
              And now we have to set the rules: not empty, string and between 5 and 55 character<br />
              $valid->notEmpty($str, "Example String")->string($str, "Example String")->between($str, 5, 55, "Example string");<br />
              echo !is_array($valid->error) ? "This string match with all the rules provided" : "The string does not match with the provided rules";<br />
              <br />
              all the error are stored in an array<br />
              if(is_array($valid->error)){
              echo"<xmp><div class="well"></xmp>";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo"<xmp></div></xmp>";
              }
              echo"<xmp><br /><br /></xmp>";
              <br /><br />
              the following example will print an error<br />
              <br />
              $str2 = 123456789;
              
              $valid->notEmpty($str2, "Example String")->string($str2, "Example String")->between($str, 5, 55, "Example string");
              echo !is_array($valid->error) ? "This string match with all the rules provided" : "The string does not match with the provided rules";
              if(is_array($valid->error)){
              echo"<xmp><div class="well"></xmp>";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo"<xmp></div></xmp>";
              }
              echo"$str";
              echo"<xmp><br /><br /></xmp>";
              

              $str3 = null;
              
              $valid->notEmpty($str3, "Example String")->string($str3, "Example String")->between($str3, 5, 55, "Example string");
              echo count($valid->error) === 0 ? "This string match with all the rules provided" : "The string does not match with the provided rules";
              if(is_array($valid->error)){
              echo"<xmp><div class="well"></xmp>";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo"<xmp></div></xmp>";
              }

                </div>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>The result</h2>
              <?php 
              $str = "<p></h1>Test's a string!</h1></p>";
              
              use \MHValidator as Valid;
              
              $valid = new Valid();
              
              $valid->notEmpty($str, "Example String")->string($str, "Example String")->between($str, 5, 280, "Example string");
              echo count($valid->error) === 0 ? "This string match with all the rules provided" : "The string does not match with the provided rules";
              if(count($valid->error) > 0){
              echo "<div class=\"well\">";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo "</div>";
              }else{
                 echo "<br />"; 
              }

              echo "<br />";
              echo "$str";
              echo "<br /><br />";
              echo "the following examples will print an error<br />";
              echo "<br /><br />";
              $str2 = 123456789;
              
              $valid->notEmpty($str2, "Example String")->string($str2, "Example String")->between($str, 5, 55, "Example string");
              echo !is_array($valid->error) ? "This string match with all the rules provided" : "The string does not match with the provided rules";
              if(is_array($valid->error)){
              echo"<div class=\"well\">";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo"</div>";
              }
              echo"<br /><br />";
              

              $str3 = null;
              
              $valid->notEmpty($str3, "Example String")->string($str3, "Example String")->between($str3, 5, 55, "Example string");
              echo !is_array($valid->error) ? "This string match with all the rules provided" : "The string does not match with the provided rules";
              if(is_array($valid->error)){
              echo"<div class=\"well\">";
              foreach($valid->error as $key => $value){
                  echo "$key: $value <br />";
              }
              echo"</div>";
              }

           
              ?>
              </p>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>With the same pattern here some example:</h2>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <td>#</td>
                            <td>Type</td>
                            <td>Value to validate</td>
                            <td>Rules</td>
                            
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>1</td>
                            <td>Number</td>
                            <td>865</td>
                            <td>Number, not empty, between</td>
                        </tr>
                        <tr>
                            <td>2</td>
                            <td>Email</td>
                            <td>info@matteohertel.com</td>
                            <td>Email, not empty</td>
                        </tr>
                        <tr>
                            <td>3</td>
                            <td>Url</td>
                            <td>http://matteohertel.com</td>
                            <td>url, not empty</td>
                        </tr>
                        <tr>
                            <td>4</td>
                            <td>Strong Password</td>
                            <td>AstrongPassword21</td>
                            <td>Strong password, not empty, between</td>
                        </tr>
                    </tbody>
                </table>    
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>Example 1:</h2>
                #1: code with no error<br />
                $number = 865;<br />
                $ex1 = new Valid;<br />
                <br />
                $ex1->number($number, "#1 number")->notEmpty($number, "#1 number")->between($number, 0, 10,  "#1 number");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex1->error);<br />
                <div class="well">
                #1: result with no error<br />
                <?php
                $number = 865;
                $ex1 = new Valid;

                $ex1->number($number, "#1 number")->notEmpty($number, "#1 number")->between($number, 0, 10,  "#1 number");
                echo"<br />";
                var_export($ex1->error);
                ?>
                </div>
                <hr />
                #1: code with error<br />
                $number = "String of more than 10 character";<br />
                $ex1 = new Valid;<br />
                <br />
                $ex1->number($number, "#1 number")->notEmpty($number, "#1 number")->between($number, 0, 10,  "#1 number");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex1_e->error);<br />
                <div class="well">
                #1: result with error<br />
                <?php
                $number = "String of more than 10 character";
                $ex1_e = new Valid;
                $ex1_e->number($number, "#1 number")->notEmpty($number, "#1 number")->between($number, 0, 10,  "#1 number");
                var_export($ex1_e->error);
                ?>
                </div>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>Example 2:</h2>
                #2: code with no error<br />
                $email = "info@matteohertel.com";<br />
                $ex2 = new Valid;<br />
                <br />
                $ex2->email($email, "#2 email")->notEmpty($email, "#2 email");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex2->error);<br />
                <div class="well">
                #2: result with no error<br />
                <?php
                $email = "info@matteohertel.com";
                $ex2 = new Valid;

                $ex2->email($email, "#2 email")->notEmpty($email, "#2 email");
                var_export($ex2->error);
                ?>
                </div>
                <hr />
                #2: code with error<br />
                $email = "infomatteohertel.com";<br />
                $ex2_e = new Valid;<br />
                <br />
                $ex2_e->email($email, "#2 email")->notEmpty($email, "#2 email");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex2_e->error);<br />
                <div class="well">
                #2: result with error<br />
                <?php
                $email = "infomatteohertel.com";
                $ex2_e = new Valid;

                $ex2_e->email($email, "#2 email")->notEmpty($email, "#2 email");
                var_export($ex2_e->error);
                ?>
                </div>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>Example 3:</h2>
                #3: code with no error<br />
                $url = "http://matteohertel.com";<br />
                $ex3 = new Valid;<br />
                <br />
                $ex3->url($url, "#3 url")->notEmpty($url, "#3 url");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex3->error);<br />
                <div class="well">
                #3: result with no error<br />
                <?php
                $url = "http://matteohertel.com";
                $ex3 = new Valid;

                $ex3->url($url, "#3 url")->notEmpty($url, "#3 url");
                var_export($ex3->error);
                ?>
                </div>
                <hr />
                #3: code with error<br />
                $url = "Hi There!"."27";<br />
                $ex3_e = new Valid;<br />
                <br />
                $ex3_e->url($url, "#3 url")->notEmpty($url, "#3 url");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex3_e->error);<br />
                <div class="well">
                #3: result with error<br />
                 <?php
                $url = "Hi There!"."27";
                $ex3_e = new Valid;

                $ex3_e->url($url, "#3 url")->notEmpty($url, "#3 url");
                var_export($ex3_e->error);
                ?>
                </div>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>Example 4:</h2>
                #4: code with no error<br />
                $pw = "AstrongPassword21";<br />
                $ex4 = new Valid;<br />
                <br />
                $ex4->strongPassword($pw, "#4 Strong Password")->notEmpty($pw, "#4 Strong Password")->between($pw, 8, 64, "#4 Strong Password");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex4->error);<br />
                <div class="well">
                #4: result with no error<br />
                <?php
                $pw = "AstrongPassword21";
                $ex4 = new Valid;
                
                $ex4->strongPassword($pw, "#4 Strong Password")->notEmpty($pw, "#4 Strong Password")->between($pw, 8, 64, "#4 Strong Password");
                var_export($ex4->error);
                ?>
                </div>
                <hr />
                #4: code with error<br />
                
                $pw = "8";<br />
                $ex4_e = new Valid;<br />
                <br />
                $ex4_e->strongPassword($pw, "#4 Strong Password")->notEmpty($pw, "#4 Strong Password")->between($pw, 8, 64, "#4 Strong Password");<br />
                echo"<xmp><br /></xmp>";<br />
                var_export($ex4_e->error);<br />
                <div class="well">
                <?php
                #4: code with error
                $pw = "8";
                $ex4_e = new Valid;
                
                $ex4_e->strongPassword($pw, "#4 Strong Password")->notEmpty($pw, "#4 Strong Password")->between($pw, 8, 64, "#4 Strong Password");
                echo"<br />";
                var_export($ex4_e->error);
                ?>
                </div>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-6">
                <h2>Feamewor-like Validator</h2>
                <h3>No error</h3>
      <br />
      $var = "STRING <xmp><h1></p><script></script></xmp>' /";<br />
       $rules = "notEmpty|string|between";<br />
      //$rules = array("1" => "notEmpty", "2" => "string", "3" => "between");<br />
      $name = "validate string";<br />
      
      $validator = new MHValidator();<br />
      $validator->max = 55;<br />
      $validator->min = 4;<br />
      $validator->validate($var, $rules, $name);<br />
      
      <br />
      var_export($validator->error);<br />
        <br />
      echo $var;<br /><br />
      
      
                <?php
      echo "<pre style='background:PaleGreen'>";
      $var = "STRING <h1></p><script></script>' /";
       $rules = "notEmpty|string|between";
      //$rules = array("1" => "notEmpty", "2" => "string", "3" => "between");
      $name = "validate string";
      
      $validator = new MHValidator();
      $validator->max = 55;
      $validator->min = 4;
      $validator->validate($var, $rules, $name);
      
      echo "<pre style='background:PaleGreen'>";
      var_export($validator->error);
      echo"</pre>";
      echo $var;
      echo"</pre>";
      ?>
                <h3>Error</h3>
      <br />
      $var = "STRING <xmp><h1></p><script></script></xmp>' /";<br />
       $rules = "notEmpty|string|between|test";<br />
      //$rules = array("1" => "notEmpty", "2" => "string", "3" => "between");<br />
      $name = "validate string";<br />
      
      $validator = new MHValidator();<br />
      $validator->validate($var, $rules, $name);<br />
      
      <br />
      var_export($validator->error);<br />
        <br />
      echo $var;<br /><br />
      
      
                <?php
      echo "<pre style='background:PaleGreen'>";
      $var = "STRING <h1></p><script></script>' /";
       $rules = "notEmpty|string|between|test";
      //$rules = array("1" => "notEmpty", "2" => "string", "3" => "between");
      $name = "validate string";
      
      $validator = new MHValidator();
      $validator->validate($var, $rules, $name);
      
      echo "<pre style='background:PaleGreen'>";
      var_export($validator->error);
      echo"</pre>";
      echo $var;
      echo"</pre>";
      ?>
            </div><!--/span-->
            <div class="clearfix"></div>
            <div class="col-12 col-sm-12 col-lg-12">
                <h2>Complete set of static validator:</h2>
             <div class="well">
                Code: echo Valid::String_($par = "String") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::String_($par = $par = "String") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Array_($par = array("one" => 1, "two" => 2)) ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Array_($par = array("one" => 1, "two" => 2)) ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Number_($par = "132") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Number_($par = "132") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Float_($par = 1.752) ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Float_($par = 1.752) ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Integer_($par = 88) ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Integer_($par = 88) ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Boolean_($par = false) ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Boolean_($par = false) ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::NotEmpty_($par = "Not Empty!") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::NotEmpty_($par = "Not Empty!") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Email_($par = "info@matteohertel.com") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Email_($par = "info@matteohertel.com") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Ip_($par = "127.0.0.1") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Ip_($par = "127.0.0.1") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Ipv4_($par = "127.0.0.1") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Ipv4_($par = "127.0.0.1") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Ipv6_($par = "::1") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Ipv6_($par = "::1") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Url_($par = "http://matteohertel.com") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Url_($par = "http://matteohertel.com") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Compare_($par = "String", "String") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Compare_($par = "String", $par = "String") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Between_($par = "123456789", 9, 9) ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Between_($par = "123456789", 9, 9) ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::Alpha_($par = "ABCDEFGH") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::Alpha_($par = "ABCDEFGH") ? "TRUE" : "FALSE");
                ?><hr />
                Code: echo Valid::StrongPassword_($par = "AstrongPassword6") ? "TRUE" : "FALSE";
                Result: <?php Valid::Debug(Valid::StrongPassword_($par = "AstrongPassword21") ? "TRUE" : "FALSE");
                ?><hr />
                <?php 
                $string = "<h1>matte 's string</h1>";
                Valid::sanitize($string);
                echo "$string";
                ?>
                <hr />
                </div>
            </div><!--/span-->
            
          </div><!--/row-->
        </div><!--/span-->
      </div><!--/row-->

      <hr>

      <footer>
            <p><script type='text/javascript'>
            copyright=new Date();
            update=copyright.getFullYear();
            document.write("&copy; Matteo Hertel - "+ update);
            </script>
            Template by <a href="http://getbootstrap.com/">Bootstrap</a></p>
      </footer>

    </div><!--/.container-->



    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="http://getbootstrap.com/assets/js/jquery.js"></script>
    <script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
    <script src="http://getbootstrap.com/examples/offcanvas/offcanvas.js"></script>
  </body>
</html>