[英]onkeypress is not working with textbox in asp.net webform
onkeypress
is not working with textbox
in asp.net webform I want to prevent special characters and alphabets onkeypress
无法与asp.net Webform中的textbox
一起使用,我想防止特殊字符和字母
Following is the aspx 以下是aspx
<wijmo:C1InputText ID="SAC" runat="server" Style="width: 150px; display: inline-block;" onkeydown="return onlyNumbers(this,event);">
</wijmo:C1InputText>
function onlyNumbers(txt, event) {
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 46) {
if (txt.value.indexOf(".") < 0)
return true;
else
return false;
}
var regex = new RegExp("^[a-zA-Z0-9]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=190)
return false;
return true;
}
It returns false but not preventing to enter. 它返回false,但不阻止其输入。 If I replaced onkeypress with onkeydown it works
如果我将onkeypress替换为onkeydown则可以正常工作
Please have a look on this post : 请看一下这篇文章:
https://forums.asp.net/t/1888076.aspx?onKeyPress+vs+OnKeyDown+vs+OnKeyUp https://forums.asp.net/t/1888076.aspx?onKeyPress+vs+OnKeyDown+vs+OnKeyUp
this is the part of the answer you want : 这是您想要的答案的一部分:
OnKeyDown allows you to fire an event prior to the character being entered within the Textbox, so that you could check what the character being pressed is prior to determining if it should be allowed or not.
OnKeyDown允许您在将字符输入到文本框中之前触发事件,以便可以在确定是否允许该字符之前检查所按下的字符是什么。
OnKeyUp will allow you to essentially hold a key down and will not fire an event until the key is released.
OnKeyUp允许您从本质上按住某个键,并且在释放该键之前不会触发事件。
OnKeyPress will fire an event for each character key that is pressed (including those being held down).
OnKeyPress将为每个被按下的字符键(包括被按下的字符键)触发一个事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.