title: How can I validate "Username" field in Laravel to contain letters, numbers, underscores and dashes?
body: <pre><code>public function store()
  {
  $this-&gt;validate(request(),[
  // Third try
  ‘username’ =&gt; ‘required|string|regex:/\w*$/|max:255|unique:users’,
  // Second try
  ‘username’ =&gt; ‘required|string|regex:/^[A-Za-z0-9]+(?:[_-][A-Za-z0-9]+)*$/|max:255|unique:users‘,
  // First try 
  ’username’ =&gt; ‘required|string|max:255’,
  ]);
  }
  </code></pre>
  <p>The username field was working well with numbers included (at first try) but then I forgot to include “unique:users”, then the form started rejecting it (redirects back with username field underlined with wriggle red line). Plus I have used laravel’s “alpha_dash” several times but keeps rejecting the input. My aim is mixture of letters, numbers, underscores and dashes.</p>
  <p>Some please help me make this right. Thanks</p>