Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using a form in my project and in that form there is a text box which should take only numbers and also only one decimal point numbers or just normal integers for ex: 10, 10.55, 11.66 or so on... it should not take more than a single decimal point for ex: 10.5.4 should not take.

I had done it using javascript as follows:

C#
function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}


this allows only numbers and also point that is "." so it takes any number of points to restrict i used like this js:

C#
function checkDec(el) {
    var ex = /^[0-9]+\.?[0-9]*$/;
    if (ex.test(el.value) == false) {
        alert('Incorrect Number');
    }


the text box i used was like this :

<input type="text" class="TextBox" runat="server" id="txtDateFrom" onkeypress="return isNumberKey(event);" onblur="checkDec(this);" />


But this is not restricting the decimal point to single decimal point. PLease help me with how i can i restrict user and allow them to enter a single decimal point .... and should i use any onclick event ? what i prefer ia an onkeypress event check ... Is that Possible please help...

Thanking You in advance!!
Posted
Updated 24-Jun-13 3:51am
v2

1 solution

use FilteredTextBox ajax control
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900