Click here to Skip to main content
13,198,727 members (40,846 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 14 Jun 2006

Regular Expression Validation Class

, 14 Jun 2006
Rate this:
Please Sign up or sign in to vote.
A class that allows easy validation of variables via Regular Expressions


I often find myself re-writing the same bits of code for various projects. I know the whole point of object oriented design is to avoid this so I made myself this little Regex validation class. I find it useful for validating inputs and database values, etc. Hope you find it useful as well. Any pointers on how to extend it or better regular expressions will be gladly accepted.

The Code

using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace Company.Validation
    public enum ValidationTypes

    /// <summary>
    /// To Validate strings against Regular Expression strings.
    /// </summary>
    public class RegExValidate
        static ArrayList RegexStrings;
        static RegExValidate()
            RegexStrings = new ArrayList();
	   RegexStrings.Insert( (int)ValidationTypes.PositiveDecimal, 
	   RegexStrings.Insert( (int)ValidationTypes.Decimal, 
	   RegexStrings.Insert( (int)ValidationTypes.PositiveNonDecimal, 
	   RegexStrings.Insert( (int)ValidationTypes.NonDecimal, 
	   RegexStrings.Insert( (int)ValidationTypes.DatesDDMMYYYY, 
	   RegexStrings.Insert( (int)ValidationTypes.DatesDDMMYY, 
	   //Dates(DD MMM YYYY)
	   RegexStrings.Insert( (int)ValidationTypes.DatesDD_MMM_YYYY, 
	 	@"^((31(?!\ (Feb(ruary)?|Apr(il)?|June?|
	 	((30|29)(?!\ Feb(ruary)?))|(29(?=\ 
		Feb(ruary)?\ (((1[6-9]|[2-9]\d)(0[48]|
	  RegexStrings.Insert( (int)ValidationTypes.Email, 
	  RegexStrings.Insert( (int)ValidationTypes.IPAddress, 
	  RegexStrings.Insert( (int)ValidationTypes.URL, 
		@"(?<protocol />\w+):\/\/(?<Domain>[\w.]+\/?)\S*");
        /// <summary>
        /// Method returns true if unknown string matches the 
        /// regular expression defined by type
        /// </summary>
        /// <param name="type">ValidationTypes enum</param>
        /// <param name="unknown">String to validate</param>
        /// <returns>True if string validates</returns>
        public static bool Process(ValidationTypes type,string unknown)
	   string test = RegexStrings[(int)type].ToString();
	   // Create a new Regex object.
	   Regex r = new Regex(RegexStrings[(int)type].ToString());
	   // Find a single match in the string.
	   Match m = r.Match(unknown);
	   return m.Success;

So it's used something like this.
You call the static method Process with the Validation Type you require and a string value of the object you are trying to validate.
This returns true if the string value is of that type and false if it's not.
For more stuff, see


  • 14th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United Kingdom United Kingdom
Have been working with computers since the early 80's when our junior school got a BBC Micro. Every since then I've been building, fixing, configuring, installing, networking, coding and designing with them. At present I mainly code web applications in C#/ASP.NET. I'm very interested in Design Patterns and try and use these generic principles in all new projects to create truly n-tier architectures. You can keep up on current affairs on

Ok so the pictures not of me, it's my little girl who makes coding everyday worth while. I just hope she becomes a lawyer not a developer.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
sachin10d1-Nov-11 23:33
groupsachin10d1-Nov-11 23:33 
Bugerror Pin
ismailqadri2023-Jun-11 20:08
memberismailqadri2023-Jun-11 20:08 
GeneralNot Running Properly Pin
pankaj.indore6-Jun-07 17:40
memberpankaj.indore6-Jun-07 17:40 
GeneralRegular Expression Class Pin
buyValu18-May-07 11:10
memberbuyValu18-May-07 11:10 
GeneralSuggestion [modified] Pin
Steve Hansen15-Jun-06 6:37
memberSteve Hansen15-Jun-06 6:37 
GeneralRe: Suggestion Pin
Cheml0ck15-Jun-06 9:05
memberCheml0ck15-Jun-06 9:05 
GeneralRange Validator Pin
correttore_automatico15-Jun-06 3:57
membercorrettore_automatico15-Jun-06 3:57 
Yes regex are nice, but I found more useful the RangeValidator for integer and double numbers. The RangeValidator is also able to validate dates, but must have day month and year.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 14 Jun 2006
Article Copyright 2006 by Cheml0ck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid