Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-24
5
Medium Priority
?
3,985 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 1000 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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 …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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