Solved

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

Posted on 2008-06-24
5
3,979 Views
Last Modified: 2013-12-16
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
Comment
Question by:Fahed_Ahmed_Shaik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 21866793
How about something like this?

if (!char.IsNumber(s, i) || s[i] != '.')
0
 
LVL 7

Assisted Solution

by:60MXG
60MXG earned 250 total points
ID: 22020531
if (!char.IsNumber(s, i))
maybe try

 if (!char.IsDigit(s, i))
????
0
 

Expert Comment

by:birbilis
ID: 22738830
try using a masked edit box WinForms control
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question