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 se开发者_JAVA技巧rver 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?
thanks
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>
EDIT:
<asp:textbox id="txtNOD" runat="server" Width="52px" Font-Size="10px"
Font-Names="Verdana" Height="16px" Enabled="False" onkeypress="return onlyNumbers();"></asp:textbox>
You can use ASP.NET validation
controls or write JavaScript
to handle text/key events or even better try out jQuery
or pluging to validate user input.
精彩评论