防止用户在ASP.NET文本框中输入非数字数据 [英] prevent user from enter non-numeric data in ASP.NET textboxes
问题描述
我正在使用VS2010/C#进行ASP.NET Web项目,我有一些文本框,用户只能在其中输入数字,如何防止用户输入非数字,浮点数和负数在我的文本框中?当然,我可以检查服务器端代码中输入的数字,但是我认为将数据发送到服务器并执行验证将花费大量时间,有没有办法防止用户输入非数字值?还是至少不将数据发送到服务器的数据不正确?
I'm working on an ASP.NET web project using VS2010/C#, I have some text boxes which users should enter only numbers in them, how can I prevent users from entering non-numeric, floating point numbers and negative numbers in my text boxes? of course I can check entered numbers in server side code, but I think sending data to server and performing validation will take a huge amount of time, is there a way to prevent users from entering non-numeric values? or at least not sending data to server with incorrect data?
谢谢
推荐答案
仅使用一些javascript并将其应用于所需的文本框,然后它将不允许除数字以外的其他文本框,请参见以下代码部分.
Simply use some javascript and apply to the text box required, then it will not allow the others except numbers, see the following code part.
<script language="JavaScript">
function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<asp:textbox id="txtNOD" runat="server" Width="52px" Font-Size="10px"
Font-Names="Verdana" Height="16px" Enabled="False" onkeypress="return onlyNumbers();"></asp:textbox>
这篇关于防止用户在ASP.NET文本框中输入非数字数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!