Html Javascript函数在按键时只输入字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16647404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:34:57  来源:igfitidea点击:

Javascript Function to enter only alphabets on keypress

javascripthtmlvalidationonkeypress

提问by Lucy

I want to enter only character values inside a <textarea>and numeric values in another. I have been able to make a JavaScript function which only allows numeric values to be entered in the <textarea>using onkeypress. This works in Firefox and Chrome.

我只想在 a 中输入字符值<textarea>,在另一个中输入数值。我已经能够制作一个 JavaScript 函数,它只允许在<textarea>using 中输入数值onkeypress。这适用于 Firefox 和 Chrome。

For alphabets I am creating another JavaScript function using windows.eventproperty. Only problem is this works only in Chrome and not in Firefox.

对于字母表,我正在使用windows.event属性创建另一个 JavaScript 函数。唯一的问题是这仅适用于 Chrome 而不适用于 Firefox。

I want to know how to allow only alphabets to be entered using onkeypressevent as used for entering only numeric values?

我想知道如何onkeypress使用用于仅输入数值的事件仅允许输入字母?

function isNumberKey(evt){  <!--Function to accept only numeric values-->
    //var e = evt || window.event;
 var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode != 46 && charCode > 31 
 && (charCode < 48 || charCode > 57))
        return false;
        return true;
 }
     
    function ValidateAlpha(evt)
    {
        var keyCode = (evt.which) ? evt.which : evt.keyCode
        if ((keyCode < 65 || keyCode > 90) && (keyCode < 97 || keyCode > 123) && keyCode != 32)
         
        return false;
            return true;
    }
<label for="cname" class="label">The Risk Cluster Name</label>
<textarea id="cname" rows="1px" cols="20px" style="resize:none" placeholder="Cluster Name" onKeyPress="return ValidateAlpha(event);"></textarea>
<br>
<label for="cnum">Risk Cluster Number:</label>
<textarea id="cmun" rows="1px" cols="12px" style="resize:none" placeholder="Cluster Number" onkeypress="return isNumberKey(event)"></textarea>

回答by Hrushi

function lettersOnly() 
{
            var charCode = event.keyCode;

            if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode == 8)

                return true;
            else
                return false;
}

<input type="text" name="fname" value="" onkeypress="return lettersOnly(event)"/>

回答by Josh Sherick

If you don't need to support older browsers I would use the inputevent. This way you can also catch non-alpha characters if the user pastes text into the textarea.

如果您不需要支持旧浏览器,我会使用该input事件。如果用户将文本粘贴到textarea.

I cleaned up your HTML a little bit. The most important changes are to the events on cnameand cnum. Note that the event in both cases has been changed to oninput.

我清理了你的 HTML 一点点。最重要的变化是对事件cnamecnum。请注意,这两种情况下的事件都已更改为oninput

<label for="cname" class="label"> The Risk Cluster Name</label>
<textarea id="cname" rows="1" cols="20" style="resize:none" placeholder="Cluster Name" oninput="validateAlpha();"></textarea>
<label for="cnum">Risk Cluster Number:</label>
<textarea id="cmun" rows="1" cols="12" style="resize:none" placeholder="Cluster Number" oninput="isNumberKey();"></textarea><br /><br /><br />

Assuming you want cnameto only accept characters in the alphabet and cnumto only accept numbers, your JavaScript should be:

假设您只想cname接受字母表中的字符并且cnum只接受数字,您的 JavaScript 应该是:

function validateAlpha(){
    var textInput = document.getElementById("cname").value;
    textInput = textInput.replace(/[^A-Za-z]/g, "");
    document.getElementById("cname").value = textInput;
}
function isNumberKey(){
    var textInput = document.getElementById("cmun").value;
    textInput = textInput.replace(/[^0-9]/g, "");
    document.getElementById("cmun").value = textInput;
}

This code uses regular expressions, a way to match patterns in strings.

此代码使用正则表达式,这是一种匹配字符串模式的方法。

回答by Siddhartha esunuri

Best Uses

最佳用途

<input type="text" name="checkno" id="checkno" class="form-control"  value="" onkeypress="return isNumber(event)"/>

<input type="text" name="checkname" id="checkname" class="form-control" value="" onkeypress="return isAlfa(event)"/>


function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

function isAlfa(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
        return false;
    }
    return true;
}

回答by vasudhendra

function digitonly(input,event){    

        var keyCode = event.which ? event.which : event.keyCode;
        var lisShiftkeypressed = event.shiftKey;
        if(lisShiftkeypressed && parseInt(keyCode) != 9){return false;}
        if((parseInt(keyCode)>=48 && parseInt(keyCode)<=57) || keyCode==37/*LFT ARROW*/ || keyCode==39/*RGT ARROW*/ || keyCode==8/*BCKSPC*/ || keyCode==46/*DEL*/ || keyCode==9/*TAB*/  || keyCode==45/*minus sign*/ || keyCode==43/*plus sign*/){return true;}     
        BootstrapDialog.alert("Enter Digits Only"); 
        input.focus();
        return false;           
}

function alphaonly(input,event){

        var keyCode = event.which ? event.which : event.keyCode;
        //Small Alphabets
        if(parseInt(keyCode)>=97 && parseInt(keyCode)<=122){return true;}
        //Caps Alphabets
        if(parseInt(keyCode)>=65 && parseInt(keyCode)<=90){return true;}
        if(parseInt(keyCode)==32 || parseInt(keyCode)==13 || parseInt(keyCode)==46 || keyCode==9/*TAB*/ || keyCode==8/*BCKSPC*/ || keyCode==37/*LFT ARROW*/ || keyCode==39/*RGT ARROW*/ ){return true;}
        BootstrapDialog.alert("Only Alphabets are allowed") 
        input.focus();
        return false; 
}

回答by RekhaShanmugam

hi try below code it worked for me in all browsers, it allows numbers and few special characters like,.+-() : in the textbox use as follows

嗨,试试下面的代码,它在所有浏览器中都对我有用,它允许数字和一些特殊字符,如,.+-() :在文本框中使用如下

<asp:Textbox Id="txtPhone" runat="server" onKeyPress="return onlyNumbersandSpecialChar()">     </asp:Textbox>

function onlyNumbersandSpecialChar(evt) {
    var e = window.event || evt;
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57 || charCode > 107 || charCode > 219 ||          charCode > 221) && charCode != 40 && charCode != 32 && charCode != 41 && (charCode < 43 || charCode > 46)) {
        if (window.event) //IE
            window.event.returnValue = false;
        else //Firefox
            e.preventDefault();
    }
    return true;

   }

 </script>