IsPhone.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class IsPhone 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. $callback = preg_replace('/[^0-9]/', '', $value);
  25. if (!preg_match("/^(02|0[3-6]\d|01(0|1|3|5|6|7|8|9)|070|080|007)\-?\d{3,4}\-?\d{4,5}$/", $callback) && !preg_match("/^(15|16|18)\d{2}\-?\d{4,5}$/", $callback)) {
  26. return false;
  27. }
  28. if (preg_match("/^(02|0[3-6]\d|01(0|1|3|5|6|7|8|9)|070|080)\-?0{3,4}\-?\d{4}$/", $callback)) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * Get the validation error message.
  35. *
  36. * @return string
  37. */
  38. public function message()
  39. {
  40. return ':attribute 은(는) 사용하실 수 없는 전화번호입니다.';
  41. }
  42. }