I have a textbox in asp.net in which I want to enter only either numeric or decimal data. The user should not be allowed to even enter any other data. I have tried it myself using the following code. But it does not allow user to enter decimal point for decimal data.
<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>
I had modified the code as below so that it allows the decimal point (period) (code=190)
if (charCode != 190)
{ ((charCode > 31 && (charCode < 48 || charCode > 57)))
return false; }
return true;
But it doesn't work as well (Here i used 190 for period as defined standard). Moreover If someone can help me limit this textbox for user to enter only 6 characters.Can anyone please correct me. I will be really thankful.
Note: I had also tried this. It also works only for numbers and also allows entering of non-numeric data but displays an error message if non-numeric data is entered. But its not what I want. I don't want user to even enter non-numeric data.
<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtBasicPay" runat="server" ErrorMessage="Only numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>