如何让textbox接受数字? [英] How to make textbox accepts numbers?
本文介绍了如何让textbox接受数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框应该只允许0到9和单个小数点(。)之间的数字,只允许正数。
解决方案
< SCRIPT language = Javascript >
<! -
函数NumberDOT(evt)
{
var charCode =(evt.which)? evt.which:event.keyCode;
if(charCode!= 46&& charCode> 31
&&(charCode< 48 || charCode> 57))
返回false;
返回true;
}
函数isNumberKey(evt)
{
var charCode =(evt.which)? evt.which:event.keyCode
if(charCode> 31&&(charCode< 48 || charCode> 57))
返回false;
返回true;
}
我希望这个链接 [ ^ ]和这一个 [ ^ ]会对你有所帮助
问候,
RK
您可以在文本框按键事件上执行此操作,也可以使用ajax控件(过滤文本)。
下面我看到了如何在按键事件中执行此操作:
private void textBox1_KeyPress( object sender,KeyPressEventArgs e)
{
if (!char .IsCo ntrol(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar!= ' 。')
{
e.Handled = true ;
}
// 只允许一个小数点
if (e.KeyChar == ' 。'
&&(sender as TextBox).Text.IndexOf('' 。')> -1)
{
e.Handled = true ;
}
}
欲了解更多详情,请参阅此链接:
http://stackoverflow.com/questions/463299/how- do-i-make-a-textbox-only-accep-numbers [ ^ ]
如果有帮助,接受答复或投票
I have a text box which should should only allow numbers between 0 to 9 and single decimal point(.) and only positive numbers.
解决方案
<SCRIPT language=Javascript> <!-- function NumberDOT(evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; }
Hi,
I hope this link[^] and this one[^] would help you a bit
Regards,
RK
You can do it on text box key press event or you can use ajax control (filter text).
Below i saw you how to do that on key press event :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } // only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
For more detail refer this link :
http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers[^]
Accept as answer or vote if help.
这篇关于如何让textbox接受数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文