allowing only alphabets in text box using java script

后端 未结 6 992
醉梦人生
醉梦人生 2020-12-02 01:28

I want to allow only alphabets in textbox using JavaScript I used the code:

var nam=f.nm.value;
if(!isNaN(nam))  
    region.innerHTML=\"alphabets only\";
<         


        
相关标签:
6条回答
  • 2020-12-02 01:43
    <html>
    <head>
        <title>allwon only alphabets in textbox using JavaScript</title>
        <script language="Javascript" type="text/javascript">
    
            function onlyAlphabets(e, t) {
                try {
                    if (window.event) {
                        var charCode = window.event.keyCode;
                    }
                    else if (e) {
                        var charCode = e.which;
                    }
                    else { return true; }
                    if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
                        return true;
                    else
                        return false;
                }
                catch (err) {
                    alert(err.Description);
                }
            }
    
        </script>
    </head>
    <body>
        <table align="center">
            <tr>
                <td>
                    <input type="text" onkeypress="return onlyAlphabets(event,this);" />
                </td>
            </tr>
        </table>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-02 01:48

    :::::HTML:::::

    <input type="text" onkeypress="return lettersValidate(event)" />
    

    Only letters no spaces

    ::::JS::::::::

    // ===================== Allow - Only Letters ===============================================================
    
    function lettersValidate(key) {
        var keycode = (key.which) ? key.which : key.keyCode;
    
        if ((keycode > 64 && keycode < 91) || (keycode > 96 && keycode < 123))  
        {     
               return true;    
        }
        else
        {
            return false;
        }
             
    }
     
    
    0 讨论(0)
  • 2020-12-02 01:54

    just use onkeypress event like below:

    <input type="text" name="onlyalphabet" onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)">
    
    0 讨论(0)
  • 2020-12-02 01:57

    You can try:

     function onlyAlphabets(e, t) {
            return (e.charCode > 64 && e.charCode < 91) || (e.charCode > 96 && e.charCode < 123) || e.charCode == 32;   
        }
    
    0 讨论(0)
  • 2020-12-02 01:58

    From kosare comments, i have create an demo http://jsbin.com/aTUMeMAV/2/

    HTML

          <form name="f" onsubmit="return onlyAlphabets()">
             <input type="text" name="nm">
             <div id="notification"></div>
             <input type="submit">
          </form>
    

    javascript

      function onlyAlphabets() {
    
      var regex = /^[a-zA-Z]*$/;
      if (regex.test(document.f.nm.value)) {
    
          //document.getElementById("notification").innerHTML = "Watching.. Everything is Alphabet now";
          return true;
      } else {
          document.getElementById("notification").innerHTML = "Alphabets Only";
          return false;
      }
    
    
    }
    
    0 讨论(0)
  • 2020-12-02 02:04

    You can use HTML5 pattern attribute to do this:

    <form>
        <input type='text' pattern='[A-Za-z\\s]*'/>
    </form>
    

    If the user enters an input that conflicts with the pattern, it will show an error dialogue automatically.

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