Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i have a text box which accepts only numeric and decimal points. so how can i display an alert message when a textbox only contains a decimal point without any numeric value on click of OK button in C#?
Posted
Updated 15-Sep-20 5:07am

C#
if (TextBox1.Text.Split('.').Length > 1)
{    // alert message for multiple dots
}
           if (TextBox1.Text.Trim() == ".")
           {
               // alert message for only dot
           }
 
Share this answer
 
Comments
Member 10593922 11-Mar-14 8:28am    
thank u for the reply. plz can u tel me how can i restrict user from entering max of only one digit after the decimal point in the texbox:

like .9--> valid
.56-->not valid
Karthik_Mahalingam 11-Mar-14 8:30am    
you can handle it in key press event
Member 10593922 11-Mar-14 8:35am    
can u plz write sample code?
Try:
C#
if (myTextBox.Text.Trim() == ".")
   {
   ...
   }
 
Share this answer
 
Comments
Ravi Bhavnani 11-Mar-14 8:32am    
I think the call to .Trim() is extraneous (and dangerous if myTextBox.Text is null) since the input filter guarantees the TextBox will contain only the period or numeric characters.

/ravi
OriginalGriff 11-Mar-14 8:49am    
The Textbox.Text property can never be null - if you use Reflector to examine the MS code, it specifically checks foe a null value and returns string.Empty instead.

The Trim is probably irrelevant, yes, but it just makes it clearer and doesn't take a lot of extra time. (And it works even if someone forgets to make sure that space is excluded later!)
Ravi Bhavnani 11-Mar-14 10:15am    
> specifically checks foe a null value and returns string.Empty
Ah. Thanks!

/ravi
OriginalGriff 11-Mar-14 10:43am    
Welcome!
C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Regex r1 = new Regex(@"^.$");
            if ((r1.IsMatch(textBox1.Text)))
            {
                MessageBox.Show("plz enter Valid value");
               //if and only if text box contains only dot without numeric value. 
            }

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900