Phone.php 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Base\Validation;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class Phone implements Rule
  5. {
  6. /**
  7. * Create a new rule instance.
  8. *
  9. * @return void
  10. */
  11. public function __construct()
  12. {
  13. //
  14. }
  15. /**
  16. * Determine if the validation rule passes.
  17. *
  18. * @param string $attribute
  19. * @param mixed $value
  20. * @return bool
  21. */
  22. public function passes($attribute, $value)
  23. {
  24. if (strlen($value) == 11) {
  25. $mobileRegex = '/^1[3-9]\d{9}$/';
  26. if (preg_match($mobileRegex, $value)) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. return false;
  33. }
  34. /**
  35. * Get the validation error message.
  36. *
  37. * @return string
  38. */
  39. public function message()
  40. {
  41. return ':attribute 必须是手机号';
  42. }
  43. }