1

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

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is Zend Framwork free? I was told that we have to pay for the framework
Yes, Zend Framework is absolutely free and open source - framework.zend.com/about/overview
1

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.