title: How to limit length and special characters of UItextField?
body: <p>I'm able to limit the length using the code below, but I can't seem to find a way to also limit special characters.</p>
  
  <pre><code>func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
  let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
  if (textField.placeholder == "USERNAME")
  {
      //Also limit special characters here
      return newLength &lt;= 15 // Bool
  }
  </code></pre>
  
  <p>characters I want allowed:</p>
  
  <blockquote>
    <p>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.</p>
  </blockquote>
  
  <p>I tried following this  <a href="https://stackoverflow.com/questions/14557925/how-to-restrict-special-characters-in-uitextfield-except-dot-and-underscores">link</a>   but its in objective C and I'm having trouble merging it with my current 15 character limit code above</p>