How to force input to only allow Alpha Letters?

后端 未结 9 1500
长发绾君心
长发绾君心 2020-12-01 06:52

using jQuery here, however unable to prevent numbers from being typed into the input field

http://codepen.io/leongaban/pen/owbjg

Input



        
相关标签:
9条回答
  • 2020-12-01 07:26

    On newer browsers, you can use:

    <input type="text" name="country_code" 
        pattern="[A-Za-z]" title="Three letter country code">
    

    You can use regular expressions to restrict the input fields.

    0 讨论(0)
  • 2020-12-01 07:27
    <input type="text" name="name" id="event" onkeydown="return alphaOnly(event);"   required />
    
    
    function alphaOnly(event) {
      var key = event.keyCode;`enter code here`
      return ((key >= 65 && key <= 90) || key == 8);
    };
    
    0 讨论(0)
  • 2020-12-01 07:32

    Short ONELINER:

    <input onkeypress="return /[a-z]/i.test(event.key)" >

    For all unicode letters try this regexp: /\p{L}/u (but ... this) - and here is working example :)

    0 讨论(0)
提交回复
热议问题