Mobile.php 607 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. declare(strict_types=1);
  3. namespace Respect\Validation\Rules;
  4. use libphonenumber\NumberParseException;
  5. use function is_scalar;
  6. final class Mobile extends AbstractRule
  7. {
  8. /**
  9. * {@inheritDoc}
  10. */
  11. public function validate($input): bool
  12. {
  13. if (!is_scalar($input)) {
  14. return false;
  15. }
  16. try {
  17. // 此处使用简单的正则表达式检查中国大陆的手机号码格式
  18. return preg_match('/^1[3456789]\d{9}$/', (string) $input) === 1;
  19. } catch (NumberParseException $e) {
  20. return false;
  21. }
  22. }
  23. }