Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

username validation

I want this rules for my username field:

  1. only a-z or A-Z
  2. 0-9
  3. underscore and dash, But should not use in start or end, Also should use only one time in word
Not valid :

__name-----

name____-----

user_____name

Valid : 

user-name
user_name

I found laravel alpha_dash helpful for this, But the problem is, it allows multiple dash and underscore also in start and end, I know we can handle this with regex but is there any quick and laravel way ?

0 likes
1 reply
Tray2's avatar

Use regex in your validation rule.

I think this one should do the trick

'user' => 'regex:/^([a-z])+?(-|_)([a-z])+$/i'

Please or to participate in this conversation.