Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 12 textboxes in my registration form out of which i want to validate only 7 textboxes. for this i have tried this code.
C#
public void ValidateFields()
        {

            var controls = new[] { txt_PartyName, txt_ContactPerson, txt_mobile1, txt_add, txt_city, txt_state, txt_PinCode };
            foreach (var control in controls.Where(e => String.IsNullOrWhiteSpace(e.Text)))
            {
                errorProvider1.SetError(control, "Please fill the required field");
            }
        }


But this code doesn't work as expected. I have call this function on my Save button click event but it is not working well. If actually i left all the field blank then also it saves data to the database i.e not required.
So plz provide me assistance to resolve this issue.
Thanks in advance
Posted

Change public void to public boolean, after the error is detected return false, if it goes all the way through return true.

Save depending on your validation result.

If (ValidateFields) {
// save to database
}


New version for validate function only IF ALL NEED TO VALIDATED:

C#
public void ValidateFields()
        {

            var controls = new[] { txt_PartyName, txt_ContactPerson, txt_mobile1, txt_add, txt_city, txt_state, txt_PinCode };

boolean isValid = true;
            foreach (var control in controls.Where(e => String.IsNullOrWhiteSpace(e.Text)))
            {
                errorProvider1.SetError(control, "Please fill the required field");
isValid = false;
            }

return isValid;
        }




If this helps, please take time to accept the solution so others may find it. Thank you.
 
Share this answer
 
v2
Comments
Member 10276989 15-Sep-14 2:52am    
How can i check function return false or true?
Sinisa Hajnal 15-Sep-14 2:55am    
I don't understand. You just change the type of the function from void to boolean and return false if the condition to validate (in your case the boxes are empty).

I'd say put return false under SetError, so if one of the boxes does not validate return immediately.

If all boxes need to be validated then put a flag...actually, too long to write, check updated solution in couple of minutes
good worked with me
Put all required Textboxes in a GroupBox.

Then validate as below.


C#
private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control control in groupBox1.Controls)
            {
                string controlType = control.GetType().ToString();
                if (controlType == "System.Windows.Forms.TextBox")
                {
                    TextBox txtBox = (TextBox)control;
                    if (string.IsNullOrEmpty(txtBox.Text))
                    {
                        MessageBox.Show(txtBox.Name+ " Can not be empty");
                    }
                }

            }
        }
 
Share this answer
 
Comments
Sinisa Hajnal 15-Sep-14 2:53am    
How this helps?
george4986 15-Sep-14 2:57am    
instead of adding new group box and checking 'groupBox1.Controls' why not using 'this.Controls'?
Prasad Avunoori 15-Sep-14 3:10am    
If you use this.Control again you need to look for those 8 Text boxes names which is cumbersome.


In future if he wants to apply validation to few more controls he can just drag them to this Group Control.

george4986 15-Sep-14 3:19am    
okay thats nice to iterate less but what if the client dont need a group box in the Form.thanks for the reply.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900