Javascript函数只能在按键上输入字母 [英] Javascript Function to enter only alphabets on keypress
问题描述
我只想在< textarea>
中输入字符值,而另一个中输入数值。我已经能够创建一个JavaScript函数,它只允许使用 onkeypress
< textarea> 中输入数值>。这适用于Firefox和Chrome。
对于字母表,我使用 windows.event
属性创建另一个JavaScript函数。只有问题是这只适用于Chrome而不适用于Firefox。
我想知道如何只允许使用 onkeypress输入字母
function isNumberKey(evt){<! - 仅接受函数数值 - > // var e = evt || window.event; var charCode =(evt.which)? evt.which:evt.keyCode if(charCode!= 46&& charCode> 31&&(charCode< 48 || charCode> 57))return false;返回true; }函数ValidateAlpha(evt){var keyCode =(evt.which)? ((keyCode< 65 || keyCode> 90)&& amp;(keyCode< 97 || keyCode> 123)&& keyCode!= 32)返回false;返回true; }
< label for =cnameclass =标签>风险群集名称< / label>< textarea id =cnamerows =1pxcols =20pxstyle =resize:noneplaceholder =Cluster NameonKeyPress =return ValidateAlpha );>< / textarea>< br>< label for =cnum>风险丛集编号:< / label>< textarea id =cmunrows =1pxcols =12px style =resize:noneplaceholder =Cluster Numberonkeypress =return isNumberKey(event)>< / textarea>
如果你不需要支持旧版本的浏览器,我可以使用 input
code>事件。这样,如果用户将文本粘贴到 textarea
中,您还可以捕获非字母字符。
你的HTML一点点。最重要的变化是 cname
和 cnum
上的事件。请注意,两种情况下的事件都已更改为 oninput
。
< label for =cnameclass =label>风险群集名称< / label>
< label for =cnum>风险集群编号:< / label>
假设您希望 cname
只接受字符在字母表和 cnum
中只接受数字,您的JavaScript应该是:
function validateAlpha(){
var textInput = document.getElementById(cname)。value;
textInput = textInput.replace(/ [^ A-Za-z] / g,);
document.getElementById(cname)。value = textInput;
}
函数isNumberKey(){
var textInput = document.getElementById(cmun).value;
textInput = textInput.replace(/ [^ 0-9] / g,);
document.getElementById(cmun)。value = textInput;
}
此代码使用正则表达式,这是匹配字符串中的模式的一种方式。
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.
For alphabets I am creating another JavaScript function using windows.event
property. Only problem is this works only in Chrome and not in Firefox.
I want to know how to allow only alphabets to be entered using onkeypress
event as used for entering only numeric values?
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>
If you don't need to support older browsers I would use the input
event. This way you can also catch non-alpha characters if the user pastes text into the textarea
.
I cleaned up your HTML a little bit. The most important changes are to the events on cname
and cnum
. Note that the event in both cases has been changed to 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 cname
to only accept characters in the alphabet and cnum
to only accept numbers, your JavaScript should be:
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.
这篇关于Javascript函数只能在按键上输入字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!