Solved

Input Validator

Posted on 2013-05-22
13
232 Views
Last Modified: 2013-05-30
Hello Experts,

I am developing  C# winform application on .NET 4.x framework. I am tired of writing validation in form level as business process keeps changing. What I am looking at one class for each form where in I can write all validations for each controls and just call the validation class to validate all controls with one single error provider. May be an user control for the class.

Not sure how to start off itself :(

Any suggestions ? Please dont suggest application validation block, fluent etc as I have already gone through those and felt it is better write my own validation class
0
Comment
Question by:GRChandrashekar
  • 7
  • 6
13 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39186792
This might be silly but how can you put ALL the validation code for every possible point of entry into one class ?  (A 10 digit string is different than a date than a currency..... and you say the requirements change).


Maybe you might want to look at developing your own self-validating controls.  For instance you could code a TextBox (based on the standard TextBox) that will only accept integers in a certain range (you supply the range at run time).
0
 

Author Comment

by:GRChandrashekar
ID: 39186996
why not develop my own class?

Say i have tbxname, i write a class pass tbxname input to class and in class define whether it should be null or not.

I can pass dteventdate to class and in class define validation for that.

similarly for all contorls. but am not sure how to start off
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187136
I think that is a bad idea or are you talking of just validating the input from one form.
0
 

Author Comment

by:GRChandrashekar
ID: 39187743
Nopes I am talking of validation of all forms and each form in diff class. I do agree it may be bad idea but not feasible to write huge code in each form
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187793
>>and each form in diff class

OK - in your original question it sounded like just one class for all forms.  That is why I criticised it at that point.

What you propose is a rather better design.


I would suggest your validation class would have one function per control, which takes the current contents of that control (not the control itself - separation of logic from GUI) and performs the necessary validation.  Return could be a string with the error message or an empty string if the validation succeeds.
0
 

Author Comment

by:GRChandrashekar
ID: 39187839
Exactly. So what I am requesting you experts is just one example to validate a text box, a list box, a radio button and a combo box so that I can understand how to go about and complete the design which i intent to
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187952
public String MyTextValidator(String inp)
{
  if(inp.Length > 10)
    return "To many chars";
  else
    return "";
}

and so on.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187959
In the form in some function to check the inputs of all controls:

String s = x.MyTextValidator(txt1.Text);
if(s.Length > 0)
  {
    //Oops - wrong input
    MsgBox.Show(s);
    return false;
   }
.....
//All is OK
return true;
0
 

Author Comment

by:GRChandrashekar
ID: 39188084
Apologies what I would prefer is loop through all controls in class level by passing parent.control from form to class and write validation for each control in the class. in form level it should just be like validatecontrols(this) which should get true or false from class
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39188098
No problems.  Just loop through the controls in the form
something like
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}
0
 

Author Comment

by:GRChandrashekar
ID: 39188151
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}

This I guess in form level !

What I need is to pass the form to class, in class loop through controls.
0
 

Author Comment

by:GRChandrashekar
ID: 39188209
Now some how I managed like this.
 public static void ClearControls(Control parent)
        {
            foreach (Control parentctrl in parent.Controls)
            {
                if (ReferenceEquals(parentctrl.GetType(), typeof (GroupBox)))
                {
                    foreach (Control parentgbxctrl in parentctrl.Controls)
                    {
                        if (parentgbxctrl.Visible)
                        {
                            if (ReferenceEquals(parentgbxctrl.GetType(), typeof (TextBox)))
                            {
                                if (parentgbxctrl.Name == "tbxdescription")
                                {
                                    if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text))
                                    {
                                       if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text))
                                       {
                                           
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

This suits my purpose. Now I need to return false after line if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text)) if validation fails and again false after line if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text)) if validation fails. if any one fails i have to return false to form. if both is true i have to return to true to form.
now if the result if false, i need to show error provider in form ( i have only one error provider} with error message for each control if there are multiples. How can I do this? This will solve my problem
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39188860
Like I said earlier - handle the controls in the form and call separate functions to validate the contents of the control.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now