Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3990
  • Last Modified:

how to allow textbox to acccept only numbers and decimal point?

i want to enter price value in my textbox.i want it to accept only numbers and one decimal point.right now its taking only numbers but not decimal point.the code i hav written is given below bt its only to accept numbers not decimal point.
 string s = txtPrice.Text;
            int chars = s.Length;
           // char p = '.';
            for (int i = 0; i < chars; i++)
            {
                if (!char.IsNumber(s, i))
                {                
                        MessageBox.Show("Enter only numbers");
                        txtPrice.Text = s.Remove(i, 1);
                        txtPrice.Select(txtPrice.Text.Length, 0);
                        return;                  
                }
0
Fahed_Ahmed_Shaik
Asked:
Fahed_Ahmed_Shaik
2 Solutions
 
Bob LearnedCommented:
How about something like this?

if (!char.IsNumber(s, i) || s[i] != '.')
0
 
60MXGCommented:
if (!char.IsNumber(s, i))
maybe try

 if (!char.IsDigit(s, i))
????
0
 
birbilisCommented:
try using a masked edit box WinForms control
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now