Validation Logic

Posted by user2961971 on Stack Overflow See other posts from Stack Overflow or by user2961971
Published on 2014-08-24T04:15:19Z Indexed on 2014/08/24 4:19 UTC
Read the original article Hit count: 131

Filed under:
|

I am trying to create some validation for a form I have. There are two text boxes and two radio buttons on the form. My logic for this validation I know is a little rusty at the moment so any suggestions would be great. Here is the code for what I have so far:

  • Keep in mind that the int errors is a public variable in the class

Start Button code:

private void btnStart_Click(object sender, EventArgs e)
{
        errors = validateForm();

        //Here I want the user to be able to fix any errors  where I am little 
        stuck on that logic at the moment 

        //validate the form
        while (errors > 0)
        {
            validateForm();
            errors = validateForm();
        }
 }

ValidateForm Method:

 private int validateForm()
    {
        errors = 0; 

        //check the form if there are any unentered values
        if (txtDest.Text == "")
        {
            errors++;
        }
        if (txtExt.Text == "")
        {
            errors++;   
        }
        if (validateRadioBtns() == true)
        {
            errors++;
        }

        return errors;

    }

ValidateRadioBtns Method:

private Boolean validateRadioBtns()
    {
        //flag - false: selected, true: none selected
        Boolean blnFlag = false;

        //both of the radio buttons are unchecked
        if (radAll.Checked == false && radOther.Checked == false)
        {
            blnFlag = true;
        }
        //check if there is a value entered in the text box if other is checked
        else if(radOther.Checked == true && txtExt.Text == "")
        {
            blnFlag = true;
        }

        return blnFlag;
    }

Overall I feel like this can somehow be more stream lined which I am fairly stuck on. Also, I am stuck on how to ensure the user can return to the form, fix the errors, and then validate again to ensure said errors are fixed.

Any suggestions would be greatly appreciated since I know this is such a nooby question.

© Stack Overflow or respective owner

Related posts about c#

Related posts about validation