Solved

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

Posted on 2008-06-24
5
3,975 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
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now