PHP Validate Filters Example

To validate data using php filter extension we need to use the PHP's filter_var() function. The basic syntax of the function is :
 filter_var(variable, filter, options)
Where the first parameter variable is the value to be filtered, the second parameter filter is the validate filter ID to be applied, and the third parameter is the array of options related to filter. The last two parameters are optional. Now lets see some example of it.

Validate Email Address :
<?php

  $email = "john@@example.com";

  if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<b>$email</b> is valid Email-Address.";
  } else {
    echo "<b>$email</b> is not valid Email-Address.";
  }

?>
Output :

john@@example.com is not valid Email-Address.

At the above example the email address is invalid because of double @@ sign.

Validate IP Addresses
<?php

  function ValidateIP($ip_addr) {
    if(filter_var($ip_addr, FILTER_VALIDATE_IP)) {
      echo "<b>$ip_addr</b> is valid IP-Address.<br/>";
    } else {
      echo "<b>$ip_addr</b> is not valid IP-Address.<br/>";
    }
  }

  ValidateIP("192.168.0.1");
  ValidateIP("192.10.0.256");

?>
Output :

192.168.0.1 is valid IP-Address.
192.10.0.256 is not valid IP-Address.

At above example we create a function ValidateIP() to validate the IP address.

Validate URL
<?php

  function ValidateURL($url) {
    if(filter_var($url, FILTER_VALIDATE_URL)) {
      echo "<b>$url</b> is valid URL.<br/>";
    } else {
      echo "<b>$url</b> is not valid URL.<br/>";
    }
  }

  ValidateURL("http://www.mycsnotes.com");
  ValidateURL("http//www.mycsnotes.com");
  ValidateURL("http://mycsnotes.com");
  ValidateURL("http:www.mycsnotes.com");

?>
Output :

http://www.mycsnotes.com is valid URL.
http//www.mycsnotes.com is not valid URL.
http://mycsnotes.com is valid URL.
http:www.mycsnotes.com is not valid URL.

At above example we create a function ValidateURL() to validate the url.

Validate Integer Values
<?php

  function ValidateINT($int) {
    if(filter_var($int, FILTER_VALIDATE_INT)) {
      echo "<b>$int</b> is valid Integer Number.<br/>";
    } else {
      echo "<b>$int</b> is not valid Integer Number.<br/>";
    }
  }

  ValidateINT(20);
  ValidateINT(20.78);
  ValidateINT(450);

?>
Output :

20 is valid Integer Number.
20.78 is not valid Integer Number.
450 is valid Integer Number.

At above example we create a function ValidateINT() to validate the integer number.

Validate Integers Within a Range
<?php

  function ValidateINT($int) {
    if(filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range" => 0,"max_range" => 100)))) {
      echo "<b>$int</b> is valid Integer Number.<br/>";
    } else {
      echo "<b>$int</b> is not valid Integer Number.<br/>";
    }
  }

  ValidateINT(20);
  ValidateINT(120);
  ValidateINT(90);
  ValidateINT(-10);

?>
Output :

20 is valid Integer Number.
120 is not valid Integer Number.
90 is valid Integer Number.
-10 is not valid Integer Number.

The above example will validate whether the supplied value is an integer or not, as well as whether it lies within the range of 0 to 100 or not.

Next Topic :