Reputation: 3279
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?
thanks
Upvotes: 2
Views: 8059
Reputation: 11844
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>
Upvotes: 3
Reputation: 94645
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.
Upvotes: 1