Tuesday, March 15, 2011

Data Validation

Problem:
Data validation is perhaps the most used feature of any application, what should I do to validate my data.
Impact:
Program might not work properly because data is not be valid.
Solution:
We could create a class that we could use to validate data. I create a class named Validator. This class contains static methods, each method takes a textbox as an argument verifies specific data type, for example string is empty or null, or if the value is integer or not or greater than or less than etc.


public static class Validator
    {
        private static ToolTip _tooltip = new ToolTip();
        public static bool IsInteger(TextBox textBox)
        {
            int value;
            if(int.TryParse(textBox.Text,out value))
            {
                textBox.BackColor = Color.White;
                _tooltip.SetToolTip(textBox, "");
                return true;
            }
            else
            {
                textBox.BackColor = Color.Maroon;
                _tooltip.SetToolTip(textBox, "Please enter a number.");
                textBox.Focus();
                textBox.Clear();
            }
            return false;
        }

        public static bool IsValidPhoneNumber(TextBox textBox)
        {
            string strRegex = @"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$";
            Regex regex = new Regex(strRegex);
            if(regex.IsMatch(textBox.Text))
            {
                textBox.BackColor = Color.White;
                _tooltip.SetToolTip(textBox, "");
                return true;
            }
            textBox.BackColor = Color.Maroon;
            _tooltip.SetToolTip(textBox, "Please enter a valid email address.");
            textBox.Focus();
            textBox.Clear();
            return false;
        }

        public static bool IsValidEmailAddress(TextBox textBox)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex regex = new Regex(strRegex);
            if(regex.IsMatch(textBox.Text))
            {
                textBox.BackColor = Color.White;
                _tooltip.SetToolTip(textBox, "");
                return true;
            }
            else
            {
                textBox.BackColor = Color.Maroon;
                _tooltip.SetToolTip(textBox, "Please enter a valid email address.");
                textBox.Focus();
                textBox.Clear();
            }
            return false;
        }

        public static bool IsDouble(TextBox textBox)
        {
            double value;
            if(double.TryParse(textBox.Text,out value))
            {
                textBox.BackColor = Color.White;
                _tooltip.SetToolTip(textBox, "");
                return true;
            }
            textBox.BackColor = Color.Maroon;
            _tooltip.SetToolTip(textBox, "Please enter a number.");
            textBox.Focus();
            textBox.Clear();
            return false;
        }

        internal static bool IsPresent(TextBox textBox)
        {
            if(!string.IsNullOrEmpty(textBox.Text))
            {
                textBox.BackColor = Color.White;
                _tooltip.SetToolTip(textBox,"");
                return true;
            }
            textBox.BackColor = Color.Maroon;
            textBox.Focus();
            _tooltip.SetToolTip(textBox,"This field is required.");
            textBox.Clear();
            return false;
        }
    }
now we could use this class as a dll library in any application and use it to validate data.

for example if we have a age text box that suppose to contain integer value, to check if text box contains we use the validator like this.

if(Validator.IsInteger(AgeTextBox))
{
    //perform some action.
}
else
{
     MessageBox.Show("Invalid integer value.");
}

1 comment:

  1. Validation is important in all kinds of applications. Very interesting article.
    Dot Net Training in Chennai
    C# Training

    ReplyDelete