PHP Validation Class

Source: Internet
Author: User
An extensible PHP validation class,
Class can be a variety of validation can be self-tuning implementation, now the basic implementation.
If you need to add a rule, define the method directly, and the method name is the rule name. Refer to the use method for specific reference.
    1. Require_once ('./validator.class.php ');
    2. $data = Array (
    3. ' Nickname ' = ' Heno ',
    4. ' Realname ' = ' Steven ',
    5. ' Age ' = 25,
    6. ' Mobile ' = ' 1521060426 ');
    7. $validator = new Validator ($data);
    8. $validator->setrule (' nickname ', ' required ');
    9. $validator->setrule (' Realname ', array (' length ' = = = Array (1,6), ' required '));
    10. $validator->setrule (' Age ', array (' required ', ' digit '));
    11. $validator->setrule (' Mobile ', Array (' mobile '));
    12. $result = $validator->validate ();
    13. Var_dump ($result);
    14. Var_dump ($validator->getresultinfo ());
Copy Code
  1. /**
  2. * Validator Data validation class
  3. * @package Library
  4. * @category Library
  5. * @author Steven
  6. * @version 1.0
  7. */
  8. /**
  9. * Validator Data validation class
  10. * @package Library
  11. * @category Library
  12. * @author Steven
  13. * @version 1.0
  14. */
  15. Class Validator {
  16. /**
  17. * Data to be verified
  18. * @var Array
  19. */
  20. Private $_data;
  21. /**
  22. * Validation rules
  23. * @var Array
  24. */
  25. Private $_rulelist = null;
  26. /**
  27. * Check Results
  28. * @var BOOL
  29. */
  30. Private $_result = null;
  31. /**
  32. * Verify data information
  33. * @var Array
  34. */
  35. Private $_resultinfo = Array ();
  36. /**
  37. * Constructor function
  38. * @param array $data The data to be verified
  39. */
  40. Public function __construct ($data = null)
  41. {
  42. if ($data) {
  43. $this->_data = $data;
  44. }
  45. }
  46. /**
  47. * Set validation rules
  48. * @param string $var with Check key
  49. * @param mixed $rule validation rules
  50. * @return void
  51. */
  52. Public Function Setrule ($var, $rule)
  53. {
  54. $this->_rulelist[$var] = $rule;
  55. }
  56. /**
  57. * Test Data
  58. * @param array $data
  59. *
  60. * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);
  61. * $validator = new Validator($data);
  62. * $validator->setRule('nickname', 'required');
  63. * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));
  64. * $validator->setRule('age', array('required', 'digit'));
  65. * $result = $validator->validate();
  66. * var_dump($validator->getResultInfo());
  67. *
  68. * @return BOOL
  69. */
  70. Public function Validate ($data = null)
  71. {
  72. $result = true;
  73. /* Returns TRUE if no validation rules are set */
  74. if ($this->_rulelist = = = NULL | |!count ($this->_rulelist)) {
  75. return $result;
  76. }
  77. /* rules are already set, then the rule is checked by article by clause */
  78. foreach ($this->_rulelist as $ruleKey = + $ruleItem) {
  79. /* If the test rule is a single rule */
  80. if (!is_array ($ruleItem)) {
  81. $ruleItem = Trim ($ruleItem);
  82. if (Method_exists ($this, $ruleItem)) {
  83. /* Verify the data, save the check results */
  84. $tmpResult = $this $ruleItem ($ruleKey);
  85. if (! $tmpResult) {
  86. $this->_resultinfo[$ruleKey] [$ruleItem] = $tmpResult;
  87. $result = false;
  88. }
  89. }
  90. Continue
  91. }
  92. /* Check rule is multiple */
  93. foreach ($ruleItem as $ruleItemKey = = $rule) {
  94. if (!is_array ($rule)) {
  95. $rule = Trim ($rule);
  96. if (Method_exists ($this, $rule)) {
  97. /* Validate data, set result set */
  98. $tmpResult = $this $rule ($ruleKey);
  99. if (! $tmpResult) {
  100. $this->_resultinfo[$ruleKey] [$rule] = $tmpResult;
  101. $result = false;
  102. }
  103. }
  104. } else {
  105. if (Method_exists ($this, $ruleItemKey)) {
  106. /* Validate data, set result set */
  107. $tmpResult = $this $ruleItemKey ($ruleKey, $rule);
  108. if (! $tmpResult) {
  109. $this->_resultinfo[$ruleKey] [$ruleItemKey] = $tmpResult;
  110. $result = false;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Get verification result data
  120. * @return [Type] [description]
  121. */
  122. Public Function Getresultinfo ()
  123. {
  124. return $this->_resultinfo;
  125. }
  126. /**
  127. * Check Required Parameters
  128. * @param string $varName checksum
  129. * @return BOOL
  130. */
  131. Public function required ($varName)
  132. {
  133. $result = false;
  134. if (Is_array ($this->_data) && isset ($this->_data[$varName])) {
  135. $result = true;
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Check parameter length
  141. *
  142. * @param string $varName checksum
  143. * @param array $lengthData Array ($minLen, $maxLen)
  144. * @return BOOL
  145. */
  146. Public function Length ($varName, $lengthData)
  147. {
  148. $result = true;
  149. /* If the item is not set, the default is check through */
  150. if ($this->required ($varName)) {
  151. $varLen = Mb_strlen ($this->_data[$varName]);
  152. $minLen = $lengthData [0];
  153. $maxLen = $lengthData [1];
  154. if ($varLen < $minLen | | $varLen > $maxLen) {
  155. $result = true;
  156. }
  157. }
  158. return $result;
  159. }
  160. /**
  161. * Verify Mail
  162. * @param string $varName checksum
  163. * @return BOOL
  164. */
  165. Public Function Email ($varName)
  166. {
  167. $result = true;
  168. /* If the item is not set, the default is check through */
  169. if ($this->required ($varName)) {
  170. $email = Trim ($this->_data[$varName]);
  171. if (Preg_match ('/^[-\w]+?@[-\w. +?$/', $email)) {
  172. $result = false;
  173. }
  174. }
  175. return $result;
  176. }
  177. /**
  178. * Check your phone
  179. * @param string $varName checksum
  180. * @return BOOL
  181. */
  182. Public Function Mobile ($varName)
  183. {
  184. $result = true;
  185. /* If the item is not set, the default is check through */
  186. if ($this->required ($varName)) {
  187. $mobile = Trim ($this->_data[$varName]);
  188. if (!preg_match ('/^1[3458]\d{10}$/', $mobile)) {
  189. $result = false;
  190. }
  191. }
  192. return $result;
  193. }
  194. /**
  195. * Calibration parameters are numeric
  196. * @param string $varName checksum
  197. * @return BOOL
  198. */
  199. Public function digit ($varName)
  200. {
  201. $result = false;
  202. if ($this->required ($varName) && is_numeric ($this->_data[$varName])) {
  203. $result = true;
  204. }
  205. return $result;
  206. }
  207. /**
  208. * Check parameters for ID card
  209. * @param string $varName checksum
  210. * @return BOOL
  211. */
  212. Public Function ID ($ID)
  213. {
  214. }
  215. /**
  216. * Check parameter is URL
  217. * @param string $varName checksum
  218. * @return BOOL
  219. */
  220. Public function URL ($url)
  221. {
  222. $result = true;
  223. /* If the item is not set, the default is check through */
  224. if ($this->required ($varName)) {
  225. $url = Trim ($this->_data[$varName]);
  226. if (!preg_match ('/^ (http[s]?::)? \w+? ( \.\w+?) $/', $url)) {
  227. $result = false;
  228. }
  229. }
  230. return $result;
  231. }
  232. }
  233. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.