none
Best Practices to Validate the User Input RRS feed

  • Question

  • Hello Everyone,

     

    I have a Forms application where user enter nformation for Example Contact Information.

    Where First Name, Last Name, Address and Phone Number for example are required information.

     

    We can have four if/else statements to check that if all the required information is present or not.

    For Example in case of First Name we can do :

    if (FirstName.Text == string.empty) // Then display message box with error

    and then so on with all other information.

     

    Say we have 50 textboxes to check for required information. In that case what is the best way to accomplish the above mentioned task ?

    Tuesday, May 15, 2007 1:56 PM

Answers

  • I have never done validation with this amount of control be here are some idea.

     

    You can loop through the me.Controls instead of validating them one by one.

    You can inherit from the textbox and have a error checking somehow.

    I like the System.Windows.Forms.ErrorProvider to handle the input error.

     

    I use .NET 1.x

    Tuesday, May 15, 2007 2:16 PM

All replies

  • If you had an array of text boxes you could iterate through them in a loop, but you would not be able to use the designer for creating the form.

     

    You could place an array of textboxes onto the form manually inside of the code-behind if you want to avoid testing each textbox individually. You could even use the designer to figure out where to place each textbox (x, y coords) before manually placing them in the code-behind.

    Tuesday, May 15, 2007 2:05 PM
  • I have never done validation with this amount of control be here are some idea.

     

    You can loop through the me.Controls instead of validating them one by one.

    You can inherit from the textbox and have a error checking somehow.

    I like the System.Windows.Forms.ErrorProvider to handle the input error.

     

    I use .NET 1.x

    Tuesday, May 15, 2007 2:16 PM