ChineseUsernameRule.php 814 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Validators\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class ChineseUsernameRule 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 (preg_match("/^[\x7f-\xff]+$/", $value) || preg_match("/^[a-zA-Z]+$/", $value)) {
  25. return true;
  26. }
  27. return false;
  28. }
  29. /**
  30. * Get the validation error message.
  31. *
  32. * @return string
  33. */
  34. public function message()
  35. {
  36. return '姓名只能是中文或英文';
  37. }
  38. }