title: What will be regex for checking alphanumeric dot dash and underschore using preg_match?
body: <p>I am checking <strong>username</strong> entered by user</p>
  
  <p>I'm trying to validate usernames in PHP using preg_match() but I can't seem to get it working the way I want it. I require preg_match() to:</p>
  
  <ul>
  <li>accept only letters , numbers and . - _</li>
  </ul>
  
  <p>i.e. alphanumeric dot dash and underscore only, i tried regex from htaccess which is like this</p>
  
  <pre><code>([A-Za-z0-9.-_]+)
  </code></pre>
  
  <p>like this way but it doesnt seem to work, it giving false for simple alpha username.</p>
  
  <pre><code>$text = 'username';
  
  if (preg_match('/^[A-Za-z0-9.-_]$/' , $text)) {
     echo 'true';   
  } else {
     echo 'false';
  }
  </code></pre>
  
  <p>How can i make it work ?</p>
  
  <p>i am going to use it in function like this</p>
  
  <pre><code>//check if username is valid
  function isValidUsername($str) {
      return preg_match('/[^A-Za-z0-9.-_]/', $str);
  }
  </code></pre>
  
  <p>i tried answwer in <a href="https://stackoverflow.com/questions/3379813/preg-match-and-username">preg_match() and username</a> but still something is wrong in the regex.</p>
  
  <hr>
  
  <p><strong>update</strong> </p>
  
  <p>I am using code given by <strong>xdazz</strong> inside function like this.</p>
  
  <pre><code>//check if username is valid
  function isValidUsername($str) {
      if (preg_match('/^[A-Za-z0-9._-]+$/' , $str)) {
         return true;   
      } else {
         return false;
      }
  }
  </code></pre>
  
  <p>and checking it like </p>
  
  <pre><code>$text = 'username._-546_546AAA';
  
  
  if (isValidUsername($text) === true) {
  echo 'good';
  }
  else{
  echo 'bad';
  }
  </code></pre>