Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:21:00+00:00 2026-06-15T14:21:00+00:00

I want textbox validation for allowing only one . value and only numbers. Means

  • 0

I want textbox validation for allowing only one . value and only numbers. Means my textbox value should take only numerics and one . value. Value should be like 123.50.
I am using a code for adding .oo or .50 value at end of my value.
My code is

double x;
double.TryParse(tb.Text, out x);
tb.Text = x.ToString(".00");

It is taking all the keys from keyboard, but I want to take only numbers and one . value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:21:02+00:00Added an answer on June 15, 2026 at 2:21 pm

    Add a Control.KeyPress event handler for your textbox.

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar))  //bypass control keys
        {
            int dotIndex = textBox1.Text.IndexOf('.');
            if (char.IsDigit(e.KeyChar))     //ensure it's a digit
            {   //we cannot accept another digit if
                if (dotIndex != -1 &&  //there is already a dot and
                    //dot is to the left from the cursor position and
                    dotIndex < textBox1.SelectionStart &&
                    //there're already 2 symbols to the right from the dot
                    textBox1.Text.Substring(dotIndex + 1).Length >= 2)
                {
                    e.Handled = true;
                }
            }
            else //we cannot accept this char if
                e.Handled = e.KeyChar != '.' || //it's not a dot or
                            //there is already a dot in the text or
                            dotIndex != -1 ||   
                            //text is empty or
                            textBox1.Text.Length == 0 || 
                            //there are more than 2 symbols from cursor position
                            //to the end of the text
                            textBox1.SelectionStart + 2 < textBox1.Text.Length;
        }
    }
    

    You may do it through designer or in your constructor like this:

    public Form1()
    {
        InitializeComponent();
        //..other initialization
        textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
    }
    

    I have also added several checks to ensure, that you could insert digits not only in the end of the text, but in any position. Same with a dot. It controls that you have not more than 2 digits to the right from the dot. I’ve used TextBox.SelectionStart Property to get the position of the cursor in the textbox. Check this thread for more info about that: How do I find the position of a cursor in a text box?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET WebForms and using some validation like the following: <asp:TextBox runat=server ID=txtUserName
i am using javascript validation library file for validating textbox values. now i want
I want to validate a string value from textbox. Validation requirements are: Exactly 2
I want to put validation on TextBox that it will only accept numeric or
I have a textbox with ID name, I want to do validation when the
I am attaching an asp.net textbox image I want my textbox should have two
I want to let TextBox control TextChanged event fire only when there are more
I want to add textbox in treeview as child of one of the parent
I want to clear textbox after ajax posting. <div style=padding: 5px; background-color: Silver;> @using
I've to make a textbox(WPF) for entering time with validation. I want to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.