UsernameRule.php 883 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Validators\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class UsernameRule 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. //用户名验证,字母开头,包含6位以上数字与字母
  25. if (preg_match("/^(?=[a-zA-Z])(?!\d+)[A-Za-z0-9_.]{6,20}$/", $value)) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /**
  31. * Get the validation error message.
  32. *
  33. * @return string
  34. */
  35. public function message()
  36. {
  37. return '字母开头,包含6位以上数字、字母及大小写';
  38. }
  39. }