/**
  * Validate a Credit Card Number
  *
  * Strips out unwanted characters, then applies a simple length test (13-16
  * digits) and the MOD 10 algorithm.
  *
  * @param string $data Field data
  * @return string Credit card number (digits only)
  * @throws FieldException
  */
 public function validate($data)
 {
     $data = parent::validate($data);
     // Strip out unwanted characters
     $data = preg_replace("|[^0-9]|", "", $data);
     // Test the number's length
     if (strlen($data) < 13 || strlen($data) > 16) {
         throw new FieldException();
     }
     // Apply the MOD 10 algorithm
     $revNumber = strrev($data);
     $numSum = 0;
     for ($i = 0; $i < strlen($revNumber); $i++) {
         $currentNum = substr($revNumber, $i, 1);
         // Double every second digit
         $currentNum = $i % 2 == 1 ? $currentNum * 2 : $currentNum;
         // Add digits of 2-digit numbers together
         if ($currentNum > 9) {
             $firstNum = $currentNum % 10;
             $secondNum = ($currentNum - $firstNum) / 10;
             $currentNum = $firstNum + $secondNum;
         }
         $numSum += $currentNum;
     }
     if ($numSum % 10 != 0) {
         // The CC number failed the MOD 10 test
         throw new FieldException();
     }
     return $data;
 }
 /**
  * Validate a Credit Card Expiration date
  *
  * Expects a date in the following format: MM/YY.
  *
  * @param string $data Field data
  * @return int Date as a timestamp value
  * @throws InvalidDateException
  */
 public function validate($data)
 {
     $data = parent::validate($data);
     // Seperate the components of the expiration date
     $components = explode("/", $data);
     if ($components[0] == $data) {
         // Seperator not found
         throw new FieldException();
     }
     // Test the length of the components
     if (strlen($components[0]) > 2 || strlen($components[1]) > 2) {
         throw new FieldException();
     }
     // Construct a timestamp that is exactly 1 second before midnight on the last
     // day of the expiration month
     $month = intval($components[0]) + 1 == 13 ? 1 : intval($components[0]);
     $year = 2000 + intval($components[1]);
     $day = 1;
     $expireTS = mktime(0, 0, 0, $month, $day, $year) - 1;
     if ($expiraTS === false || $expireTS == -1 || $expireTS < time()) {
         // The expire date is either invalid or already past
         throw new FieldException();
     }
     return $data;
 }
Example #3
0
 /**
  * Validates a given value thereby considering the state of the field
  * that a specific validator instance is related to.
  *
  * @param mixed $value
  *
  * @return boolean
  */
 public function validate($value)
 {
     $is_valid = parent::validate($value);
     if (true === $is_valid) {
         $is_valid = 1 === preg_match('/^\\{?[0-9a-f]{8}\\-?[0-9a-f]{4}\\-?[0-9a-f]{4}\\-?' . '[0-9a-f]{4}\\-?[0-9a-f]{12}\\}?$/i', $value);
     }
     return $is_valid;
 }
 /**
  * Validate a Email Field
  *
  * Email fields are text fields that must contain a legal e-mail address.
  *
  * @param string $data Field data
  * @return string Data in MD5 form
  * @throws FieldException, FieldSizeException
  */
 public function validate($data)
 {
     $data = parent::validate($data);
     if (!preg_match('"^[-!#$%&\'*+\\\\./0-9=?A-Z^_`a-z{|}~]+' . '@' . '[-!#$%&\'*+\\\\/0-9=?A-Z^_`a-z{|}~]+\\.' . '[-!#$%&\'*+\\\\./0-9=?A-Z^_`a-z{|}~]+$"', $data)) {
         // Not a valid email address
         throw new EmailFieldException();
     }
     return $data;
 }
Example #5
0
 public function validate($value)
 {
     if (!parent::validate($value)) {
         return false;
     }
     if (!empty($value)) {
         return filter_var((string) $value, FILTER_VALIDATE_EMAIL);
     }
     return true;
 }
 /**
  * Validates a given value thereby considering the state of the field
  * that a specific validator instance is related to.
  *
  * @param mixed $value
  *
  * @return boolean
  */
 public function validate($value)
 {
     if (is_array($value)) {
         foreach ($value as $text) {
             if (!parent::validate($text)) {
                 return false;
             }
         }
     } elseif (!is_null($value)) {
         return false;
     }
     return true;
 }
 /**
  * Validate a Telephone Field
  *
  * A telephone numbers may only contain numbers, the following charaters, and a space:
  *   + ( ) -
  * Any other characters will throw an InvalidTelephoneException.  Any of the above
  * characters will be thrown out and the resulting string will be in the following
  * format:
  *  +y-x
  * Where 'y' is the international calling code and 'x' is the phone number.  If
  * there is no '+y' in the string then the international calling code is assumed
  * to be +1 (United States).  If there is a '+y' in the string, it must be followed
  * by either a '-' or a ' ', then a telephone number.
  *
  * @param string $data Field data
  * @return string This function may alter data before validating it, if so this is the result
  * @throws InvalidTelephoneException
  */
 public function validate($data)
 {
     $data = parent::validate($data);
     $data = $this->validatePhoneNumber($data);
     return $data;
 }
 /**
  * Validate a Password Field
  *
  * A password field is the same as a text except that the data is returned as
  * a MD5 hash.
  *
  * @param string $data Field data
  * @return string Data in MD5 form
  * @throws FieldException, FieldSizeException
  */
 public function validate($data)
 {
     return md5(parent::validate($data));
 }
 /**
  * Validate a Date Field
  *
  * Date's may only contain numbers, seperated by a '/' or a '-', be in one of
  * the following formats: MM/DD/YYYY, DD/MM/YYYY (the year may also be 2 digits
  * instead of 4), and must be a legal date.
  *
  * @param string $data Field data
  * @return int Date as a timestamp value
  * @throws InvalidDateException
  */
 public function validate($data)
 {
     $data = parent::validate($data);
     $data = $this->validateDate($data);
     return $data;
 }