?
Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 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