CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: form validation

  1. #1
    Join Date
    May 2005
    Posts
    195

    form validation

    Hi. I am validating some controls on a form using each control's validating property. If I need to raise an error, I set the error provider to that control with a message... How can I prevent actions from happening when the user presses a button (submit button for instance) if there is an error on the screen?

    Also, how can I create a set of common functions so I do not have to duplicate code for required fields, or numeric fields, ect..

    I am not talking about creating a control which acts as the asp.net validation fields, I am wanting to implement a more structured error handling logic, using iDataErrorProvider ect, but I am totally lost on how to implement.

    A simple commonFunctions class would be nice, but again, I am very lost on how to do this..

    Thanks,

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: form validation

    you can loop the controls/fields on submit and then check each control with the error provider. if an empty/zero-length string was returned by the error provider for a control, it means there was no error, otherwise an error was occured on the control/field.

    Code:
    for each field as control in me.controls
       if control.name like "txt*" then ' <-- assume field names start with "txt"
          if errProvider.geterror(control).length <> 0 then
             '  codes to resolve such error
          end if
       end if
    next
    as for the required field checking, you can also use the above code.

    Code:
    for each field as control in me.controls
       if control.name like "txt*" then ' <-- assume field names start with "txt"
          if errProvider.geterror(control).length <> 0 then
             '  code to resolve such error
          else if (<some indication for required field>) andalso control.value.length = 0 then
             ' code to resolve such error
          end if
       end if
    next
    Busy

  3. #3
    Join Date
    May 2005
    Posts
    195

    Re: form validation

    Thanks thread, your reply is very helpful.

  4. #4
    Join Date
    May 2005
    Posts
    195

    Re: form validation

    I do have one more problem.

    I am able to loop through the controls to see which ones have an error provider message, ect.. However, how do I loop through each control, and start them off as invalid (such as required fields)?

    Or, let their error be set to "", but if they try to submit the screen without filling out the required fields, it will validate the controls, then raise a warning or block the user?

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: form validation

    at first, you may put a mark/indication to the controls/textboxes that you think required/mandatory. you may use the Tag property which is present on most form controls. let say, textboxe's with a string value "r" on their Tag property are required fields. so in our sample code (2nd) you would change it to

    Code:
    for each field as control in me.controls
       if control.name like "txt*" then ' <-- assume field names start with "txt"
          if errProvider.geterror(control).length <> 0 then
             '  code to resolve such error
          else if "r".compareto(control.tag) = 0 andalso control.value.length = 0 then
             ' code to resolve such error
          end if
       end if
    next
    hope you'll find it helpful
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured