Check if MaxLength is less than 5 for Textbox

I am trying to validate a textbox on my form. I want to make sure I cover all possible things that go wrong such as: not entering anything and entering a number less than 5 digits. I set the textbox MaxLength=5, which works fine since if I do go over 5 digits, I don't even see them on the screen the program just keeps beeping since I already set the value to 5. But, I'm having trouble getting the messagebox to show if the user enters a number with less than 5 digits. I know this has something to do with the max length but I don't know how to fix this. Is it not possible to test a textbox for anything less than it's set max length?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (txtNumber.Text == "")
            {
                MessageBox.Show("Please enter your number ");
            }
            else if (txtNumber.MaxLength > 5)
                    MessageBox.Show("Please make sure account number is exactly 5 digits");
                txtNumber.Focus();
            }
        }  
}

*Note: I have seen various versions of this with using things like the digit method and keypress but I want to keep this as simple as possible and just use maxlength.

2 answers

  • answered 2020-09-24 15:58 Idle_Mind

    I would just check for blank, and then if the Length is NOT exactly 5. This would cover both scenarios. Note that I'm checking the length of the Text in the TextBox itself with txtNumber.Text.Length != 5:

    private void btnEnter_Click(object sender, EventArgs e)
    {
        if (txtNumber.Text == "")
        {
            MessageBox.Show("Please enter your number ");
        }
        else if (txtNumber.Text.Length != 5)
            MessageBox.Show("Please make sure account number is exactly 5 digits");
            txtNumber.Focus();
        }
        else 
        {
            // ... input was good, do something in here? ...
        }
    }  
    

    You could also use TextLength direclty.

  • answered 2020-09-24 16:04 William Nordlund

    In your else if statement you check the TextBox.MaxLength property

    The maximum number of characters that can be manually entered into the text box. The default is 0, which indicates no limit.
    For more information, see TextBox.MaxLength Property

    If you already set the property to 5 (in forms designer) or just in code it won't change (unless you have another code somewhere that changes this to another number).

    TextBox.MaxLength property has nothing to do with TextBox.Text.Length or TextBox.TextLength which both is the count of characters in the text.

    The number of characters contained in the text of the control.
    For more information, see TextBox.TextLength Property

    I would code it something like this:

    private void btnEnter_Click(object sender, EventArgs e)
        {
            if (txtNumber.Text == "")
            {
                MessageBox.Show("Please enter your number ");
            }
            else if (txtNumber.TextLength < 5)
                    MessageBox.Show("Please make sure account number is exactly 5 digits");
                txtNumber.Focus();
            }
        }