Click here to Skip to main content
12,398,816 members (56,883 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
How can i make a textbox which takes only double numbers??
Posted 11-Jan-10 22:08pm
Updated 11-Jan-10 22:18pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
Smile | :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 | [Rose]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 12 Jan 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100