Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox validation in a Windows Form

I want to put a validation that the user always enters a value in the textbox before submiting the form. But the check that I have put allows user to enter white spaces and continue submitting the form. So, how to put the check so that the user in not able to submit the form if there are only white spaces in the textbox.

like image 670
Rishabh Ohri Avatar asked Nov 17 '10 07:11

Rishabh Ohri


1 Answers

You can make your own custom validation function. This may be very naive, but somehow it will work.

private bool WithErrors()
{
    if(textBox1.Text.Trim() == String.Empty) 
        return true; // Returns true if no input or only space is found
    if(textBox2.Text.Trim() == String.Empty)
        return true;
    // Other textBoxes.

    return false;
}

private void buttonSubmit_Click(object sender, EventArgs e)
{
    if(WithErrors())
    {
        // Notify user for error.
    }
    else
    {
        // Do whatever here... Submit
    }
}
like image 95
yonan2236 Avatar answered Sep 27 '22 15:09

yonan2236