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 193031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:28:34+00:00 2026-05-11T16:28:34+00:00

I am trying to prevent a user from entering anything into a particular textbox

  • 0

I am trying to prevent a user from entering anything into a particular textbox aside from a number or a period in C#. The textbox is supposed to contain an IP address. I have it working such that non-numeric entries are prevented, however I can’t seem to get it to allow a period to be entered. How might I accomplish this?

    private void TargetIP_KeyDown(object sender, KeyEventArgs e)
    {
        // Initialize the flag to false.
        nonNumberEntered = false;

        // Determine whether the keystroke is a number from the top of the keyboard.
        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            // Determine whether the keystroke is a number from the keypad.
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                // Determine whether the keystroke is a backspace.
                if (e.KeyCode != Keys.Back)
                {
                    nonNumberEntered = true;
                    errorProvider1.SetError(TargetIP, FieldValidationNumbersOnly);
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                }
            }
        }

        //If shift key was pressed, it's not a number.
        if (Control.ModifierKeys == Keys.Shift)
        {
            nonNumberEntered = true;
        }
    }


    private void TargetIP_KeyPress(object sender, KeyPressEventArgs e)
    {
        // Check for the flag being set in the KeyDown event.
        if (nonNumberEntered == true)
        {
            // Stop the character from being entered into the control since it is non-numerical.
            e.Handled = true;
        } 

        else
        {
            errorProvider1.Clear();
        }
    }
  • 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-05-11T16:28:34+00:00Added an answer on May 11, 2026 at 4:28 pm

    You should use a MaskedTextBox control instead of a normal TextBox control. Then just set the Mask property to 990\.990\.990\.990 and you are done.

    • 9 optional digit or space
    • 0 required digit
    • \. escaped dot

    UPDATE

    While I suggested using MaskedTextBox, I never (at leat I cannot remeber) used it myself. Now I just tried it … well, forget what I said. I thought it might be a simple solution, but it is an unusable solution, too.

    New, more complex, but far better suggestion and the way I usually do it.

    • Create a custom control with four text boxes, three labels with a dot each between them.

    • Analyse the input.

      • Just pass control keys to the control.
      • Move the focus if the user enters a dot, a tab (will be handled by the control), a space, may be a fourth digit.
      • Discard everything that is not a digit.
      • Discard the digit if the number falls out of the range from 0 to 255.
      • Change the text to 0 if the user deletes the last digit.
      • Remove a leading zero if the user enters a non-zero digit.
    • Add a property to the user control that returns the entered address.

    • May be make the proeprty writable and add event handling.

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

Sidebar

Related Questions

I am trying to secure a controller action to prevent a user from accessing
I am trying to prevent the user from pinning my .NET app to the
I'm trying to prevent a javascript file from loading if a user is on
I am trying to prevent the user from resizing (including maximizing) the window of
So I am trying to prevent the user from being able to use the
I'm trying to prevent a user from disabling style.(I HAVE A GOOD REASON TO
I'm trying to do this in a way that will prevent end-user browser security
I'm trying to prevent A HREF from actually opening the link, but to execute
I'm trying to prevent information to be copied from a page (for non-technical users
I am trying to prevent VS from breaking on JS errors. I have the

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.