Solved

Input Validator

Posted on 2013-05-22
13
234 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 64
SqlDataBase 7 53
.NET 2008 VB and C# 6 39
How to read text with RegEx... 2 25
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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