Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can i make a textbox which takes only double numbers??
Posted
Updated 11-Jan-10 22:18pm
v2

Usually the approach is different: you validate the user input, accepting only values representing double numbers.
On invalid input a message error is prompted to the user.
:)
 
Share this answer
 
I think you need to validate the number when keypress event is triggered for the textbox.

Just use Javascript to do that :

function check_double(e,fieldid)
{
     var field = document.getElementById(fieldid);
     if (!(((e.keyCode>=48)&&(e.keyCode<=57))||(e.keyCode==46)))
     {
        e.returnValue = false;        
        e.keyCode=0;                   
     }     
     if (e.keyCode==46)
     {
        var patt1=new RegExp("\\.");        
        var ch =patt1.exec(field);       
        if(ch==".")
        {
            e.returnValue = false;
            e.keyCode=0;
        }                        
     }
} 

Now in your Page_Load use this :

TextBox.Attributes.Add("onkeypress", "javascript:check_double(event,'" + TextBox.ClientID + "');");


Just place this for every textbox for which you want to validate Double.

I hope you will like the solution.
Cheers
:rose:
 
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