Search code examples
javascripthtmlregexrequired

input to only accept alpha chars (no numbers, or special)


I am trying to limit a text input field to only accept alphabetical characters (uppercase and lowercase is fine; but no numbers or special chars). I am trying the below for instance:

<input id='style-name'type="text" data-value-field="value" name="styleName" required pattern="^[a-z]$"/>

What am I doing incorrect here? Ideally, I would just do this on the HTML side.


Solution

  • You should add plus + sign, to match one or more occurrences of characters typed in input:

    <input id='style-name'type="text" data-value-field="value" name="styleName" required pattern="^[A-Za-z]+$"/>