Friday, July 15, 2011

Validation Class For String

Extension method is introduced by Microsoft in .Net 3.5. With help of extension method, developer can extend any class without change in original source code. Extension methods are static methods that can appear to be part of a class without actually being in the source code for the class. So extension method is one of the richest feature of .net 3.5.

Here I create class for string validation which extends string class. With help of bellow class we can validate some regular string validation like email, password , numeric etc. It provide facility to validate your own custom regular expression.

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;



namespace validation
{
    public static class StringValidationWithExtension
    {

        //built in regular expresssion
        private static Regex RegValidationNumeric = new Regex(@"^[0-9]*[.]{0,1}[0-9]*$");
        private static Regex RegValidationAlphabets = new Regex(@"^([a-zA-Z]+)$");
        private static Regex RegValidationPostCode = new Regex(@"^([a-zA-Z0-9_\s\-]*)$");
        private static Regex RegValidationInteger = new Regex(@"^[0-9]*$");
        private static Regex RegValidationEmail = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*");
        private static Regex RegValidationPassword = new Regex(@".*(?=.{6,})(?=.*\d).*");

        //Alphabet Validation
        public static bool ValidAlphabateOnly(this string alphaString)
        {
            return ValidStringWithCustomRegex(alphaString, RegValidationAlphabets);
        }

        //Numeric validation
        public static bool ValidNumeric(this string numStr)
        {
            return ValidStringWithCustomRegex(numStr, RegValidationNumeric);
        }

        //interget validation
        public static bool ValidInteger(this string intStr)
        {
            return ValidStringWithCustomRegex(intStr , RegValidationInteger);
        }

        //Postal Code Validation
        public static bool ValidPostalCode(this string postalCodeString)
        {
            return ValidStringWithCustomRegex(postalCodeString, RegValidationPostCode);
        }

        // Email Validation
        public static bool ValidateEmail(this string emailStr)
        {
            return ValidStringWithCustomRegex(emailStr, RegValidationEmail);
        }

        //validate password with 6 numeric string
        public static bool ValidatePassword(this string pwdStr)
        {
            return ValidStringWithCustomRegex(pwdStr, RegValidationPassword);
        }

        //check with string custom regular expression
        public static bool ValidStringWithCustomRegex(this string regString, Regex regExStatement)
        {
            if (regString.Length == 0)
            {
                return false;
            }

            if (regExStatement.IsMatch(regString))
            {
                return true;
            }
            return false;
        }
    }
}

How to use :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace validation
{
    class Program
    {
        static void Main(string[] args)
        {
            string test1 = "alphatesting";
            string test2 = "al12252@test.com";
            string test3 = "1111.555";

            //now string class extends with all new validation meathod
            Console.WriteLine(test1.ValidAlphabateOnly()); //return true
            Console.WriteLine(test2.ValidAlphabateOnly()); //return false

            Console.WriteLine(test3.ValidNumeric()); //return true
            Console.WriteLine(test3.ValidInteger()); //return false

            Console.WriteLine(test1.ValidateEmail()); //return false
            Console.WriteLine(test2.ValidateEmail ()); //return true
        }
    }
}
Enjoy with this validation class

No comments:

Post a Comment