如何限制asp.net文本框中的字符 [英] How to restrict characters in the asp.net textbox
本文介绍了如何限制asp.net文本框中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制asp.net文本框中的字符
我写了脚本,但是不起作用,
以下代码有什么问题
How to restrict characters in the asp.net textbox
i wrote the script but its not working,
whats wrong in the following code
function numberonly(event)
{
var numonly=(event.which) ? event.which : event.keyCode
if(numonly > 31 && (numonly < 97 || numonly > 122))
return true;
return false;
}
推荐答案
应该怎么做?
如果您只想要数字,那为什么不使用Char.IsDigit
[
What is supposed to do your function?
if you want just digits then why don''t you use theChar.IsDigit
[^] method?
:)
好,这是我的操作方法.您需要对其进行修改以适合您的需求
Well Here is how I did it. You need to modify it to suit your needs
function allowOnlyDigit(evt, obj)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 45 )
{
var val = obj.value;
if(val.length > 0)
return false;
}
else if (charCode > 31 && (charCode < 48) || (charCode > 57))
{
return false;
}
return true;
}
onkeypress="return allowOnlyDigit(event, this)"
您可以使用以下JS函数:
You can use following JS Function :
function DisableSplChars(e)
{
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event)
{
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "!" || keychar == "@" || keychar == "#"
|| keychar == "
这篇关于如何限制asp.net文本框中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文