Monday, November 7, 2011

server side validation using regular expression and extension method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DAL
{
public static class Extention
{
const string MatchEmailPattern =
@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

const string NumberPattern=@"^\d$";
const string AlphbetPattern = @"^[a-zA-Z]*$";
const string AlphanumricPattern = @"^[a-zA-Z0-9]*$";
const string AlphNumeric_space = @"[^\w\s]";
const string AlphaNumeric_spacial = @"^((?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&[^#])*&?)$";
const string AlphaNumeric_Hyphen = @"^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$";
//Date Constant/////////////////
const string Dateyyyydddmm_pattern = @"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$";
const string Datemmddyyyy_Pattern = @"^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$";
const string Dateddmmyyyy_Pattern = @"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";
const string DateddMMMyyyy_Pattern = @"^(0[1-9]|1[012])[- /.](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[-
/.](19|20)\d\d$";

const string
Url_Pattern=@"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,|
|>|<|;|\)])";
public static bool IsEmail(this string value)
{
if (value != null)
return System.Text.RegularExpressions.Regex.IsMatch(value, MatchEmailPattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsNumber(this string value)
{
if (value != string.Empty)
return System.Text.RegularExpressions.Regex.IsMatch(value, NumberPattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;

}
public static bool IsAlphabet(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, AlphbetPattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsAlphaNumeric(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, AlphanumricPattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsAlphaNumeric_Space(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, AlphNumeric_space,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsAlphaNumeric_Spacial(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, AlphaNumeric_spacial,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsAlphaNumeric_Hyphen(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, AlphaNumeric_Hyphen,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsDateyyyydddmm(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, Dateyyyydddmm_pattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}

public static bool IsDatemmddyyyy(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, Datemmddyyyy_Pattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsDateddmmyyyy(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, Dateddmmyyyy_Pattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsDateddMMMyyyy(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value,
DateddMMMyyyy_Pattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
public static bool IsUrl_Valid(this string value)
{
if (!string.IsNullOrEmpty(value))
return System.Text.RegularExpressions.Regex.IsMatch(value, Url_Pattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
else
return false;
}
}

}
AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath