开发者

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 开发者_JS百科that I can use rather than write all validation method manually?

Thank you


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


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


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


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);
  }
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜