q0987
q0987

Reputation: 35984

PHP - Server side validation method

I am using jQuery Form Validation Plugin to do comprehensive client side validation.

Now I would like to use PHP to do the server side validation.

Check whether the email is email, age is in the right range, etc.

Is there similar package or function collections that I can use rather than write all validation method manually?

Thank you

Upvotes: 1

Views: 378

Answers (4)

user9936320
user9936320

Reputation:

These are a couple of methods I use

<?php
  function chkReq($fields) {
    foreach($fields as $name) {
      if(req($name)) {
        global $values;
        $values[$name] = $_POST[$name];
      } else {
        global $errors;
        $errors[$name] = "This field is required";
      }
    }
  }

  function req($name) {
    if(isset($_POST[$name]) && !empty($_POST[$name])) {
      return true;
    } else {
      return false;
    }
  }

  function chkDate($name, $from, $to) {
    global $errors;
    global $values;
    if(!req($name)) {
      $errors[$name] = "This field is required";
    } elseif(!betweenNumbers($_POST[$name], $from, $to)) {
      $errors[$name] = "Value must be between $from en $to .";
      $values[$name] = $_POST[$name];
    } else {
      $values[$name] = $_POST[$name];
    }
  }

  function betweenNumbers($value, $from, $to) {
    if($value >= $from && $value <= $to) {
      return true;
    } else {
      return false;
    }
  }

  function chk3Fields($field1, $field2, $field3) {
    global $errors;
    global $values;
    if(!req($field1) && !req($field2) && !req($field3)) {
      $errors[$field1] = "One of the three fields is required";
      $errors[$field2] = "One of the three fields is required";
      $errors[$field3] = "One of the three fields is required";
    } else {
      $values[$field1] = $_POST[$field1];
      $values[$field2] = $_POST[$field2];
      $values[$field3] = $_POST[$field3];
    }
  }

  function checkRegistry($name){
    global $errors;
    global $values;
    if(!req($name)) {
      $errors[$name] = "This field is mandatory";
    } elseif(!validRegistry($_POST[$name])) {
      $errors[$name] = "This is not a valid registry number";
      $values[$name] = $_POST[$name];
    } else {
      $values[$name] = $_POST[$name];
    }
  }

  function validRegistry($value) {
    $value = preg_replace("/(\.|-)/", "", $value);
    $firstPart= substr($value, 0,9);
    $residueFirstPart= $firstPart% 97;
    $calculatedControlNr = 97 - $residueFirstPart;
    $obzervedControlNr = substr($value, 9, 2);
    return ($calculatedControlNr == $obzervedControlNr);
  }
?>

Upvotes: 0

Ankur Mukherjee
Ankur Mukherjee

Reputation: 3867

for email validation you can use is_email function. Check the link:http://www.ohloh.net/p/isemail

Upvotes: 1

Artefacto
Artefacto

Reputation: 97805

The closest thing that's bundled with PHP is the filter extension. It does validation and sanitization.

Upvotes: 1

Alex N.
Alex N.

Reputation: 15935

If you're writing more or less complex application, you probably would benefit from using a framework, such as Zend Framework for instance. It has some standalone classes to aid validation process as well, for example Zend_Validate: http://framework.zend.com/manual/en/zend.validate.html

Upvotes: 1

Related Questions