Overview

Namespaces

  • None
  • PHP
  • Wei
    • Validator

Classes

  • All
  • AllOf
  • Alnum
  • Alpha
  • BaseValidator
  • Between
  • Blank
  • Callback
  • CharLength
  • Chinese
  • Color
  • Contains
  • CreditCard
  • Date
  • DateTime
  • Decimal
  • Digit
  • Dir
  • DivisibleBy
  • DoubleByte
  • Email
  • EndsWith
  • EqualTo
  • Exists
  • FieldExists
  • File
  • GreaterThan
  • GreaterThanOrEqual
  • IdCardCn
  • IdCardHk
  • IdCardMo
  • IdCardTw
  • IdenticalTo
  • Image
  • In
  • Ip
  • Length
  • LessThan
  • LessThanOrEqual
  • Lowercase
  • Luhn
  • MaxLength
  • MinLength
  • MobileCn
  • NaturalNumber
  • NoneOf
  • Null
  • Number
  • OneOf
  • Password
  • Phone
  • PhoneCn
  • PlateNumberCn
  • PositiveInteger
  • PostcodeCn
  • Present
  • QQ
  • RecordExists
  • Regex
  • Required
  • SomeOf
  • StartsWith
  • Time
  • Tld
  • Type
  • Uppercase
  • Url
  • Uuid
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Wei Framework
 4:  *
 5:  * @copyright   Copyright (c) 2008-2013 Twin Huang
 6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
 7:  */
 8: 
 9: namespace Wei\Validator;
10: 
11: /**
12:  * Check if the input is valid Hong Kong identity card
13:  *
14:  * @author      Twin Huang <twinhuang@qq.com>
15:  */
16: class IdCardHk extends BaseValidator
17: {
18:     protected $invalidMessage = '%name% must be valid Hong Kong identity card';
19: 
20:     protected $negativeMessage = '%name% must not be valid Hong Kong identity card';
21: 
22:     /**
23:      * {@inheritdoc}
24:      */
25:     protected function doValidate($input)
26:     {
27:         if (!$this->isString($input)) {
28:             $this->addError('notString');
29:             return false;
30:         }
31: 
32:         if (8 != strlen($input)) {
33:             $this->addError('invalid');
34:             return false;
35:         }
36: 
37:         $input = strtoupper($input);
38: 
39:         // The first char should be A-Z
40:         $first = ord($input[0]);
41:         if ($first <  65 || $first > 90) {
42:             $this->addError('invalid');
43:             return false;
44:         }
45: 
46:         // c1 = ord(c1) - 64 => A=1, B=2, ... , Z=26
47:         // sum = c1*8 + c2*7 + ... + c6*3 + c5*2
48:         $sum = ($first - 64) * 8;
49:         for ($i = 1, $j = 7; $i < 7; $i++, $j--) {
50:             $sum += $input[$i] * $j;
51:         }
52: 
53:         $checksum = $sum % 11;
54:         if ($checksum == 1) {
55:             $checksum = 'A';
56:         } elseif ($checksum > 1) {
57:             $checksum = 11 - $checksum;
58:         }
59: 
60:         if ($checksum != $input[7]) {
61:             $this->addError('invalid');
62:             return false;
63:         }
64: 
65:         return true;
66:     }
67: }
68: 
Wei Framework API documentation generated by ApiGen