|
-
September 4th, 2007, 02:50 PM
#1
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,
-
September 4th, 2007, 08:51 PM
#2
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 
-
September 5th, 2007, 07:07 AM
#3
Re: form validation
Thanks thread, your reply is very helpful.
-
September 5th, 2007, 12:47 PM
#4
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?
-
September 5th, 2007, 10:53 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|