Server Side Validation Class Class Name= validate.cs
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
/// Summary description for validate
/// </summary>
public class validate
{
public validate()
{
}
public static IDictionary<string, Regex> RegexDictionary = new Dictionary<string, Regex>() {
{ "Phone", new Regex("\\d{10}")},
{ "Email", new Regex(@"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$"
)},
{ "URL", new Regex(@"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?")},
{ "ZipCode", new Regex("^[0-9]{6}$")}
};
public string IsPhoneValid(string phone)
{
if (!RegexDictionary["Phone"].IsMatch(phone))
{
// Phoneno should be digits only
return "Phoneno Must be of 10 digits only";
}
else
{
return "";
}
}
public string IsEmailValid(string email)
{
if (!RegexDictionary["Email"].IsMatch(email))
{
// EmailId is invalid
return "EmailId is invalid";
}
else
{
return "";
}
}
public string IsURLValid(string url)
{
if (!RegexDictionary["URL"].IsMatch(url))
{
// URL is invalid
return "URL is invalid";
}
else
{
return "";
}
}
public string IsZipCodeValid(string url)
{
if (!RegexDictionary["ZipCode"].IsMatch(url))
{
// ZipCode must be of 6 digit only.
return "ZipCode must be of 6 digit only.";
}
else
{
return "";
}
}
public string Minimuntwochar(string name)
{
Regex objAlphaPattern = new Regex("^[A-Za-z]{2,}$");
if (!objAlphaPattern.IsMatch(name))
{
// Minimum Two Characters.
return "Minimum Two Characters.";
}
else
{
return "";
}
}
public string Isnullorempty(String Controlname,string name)
{
if (string.IsNullOrEmpty(name))
{
return "Please Enter " + Controlname;
}
else
{
return "";
}
}
public string Compare(string controlname,string first,string second)
{
//Compare two field
if (!string.IsNullOrEmpty(first))
{
if (first == second)
{
return controlname + " accepted";
}
else
{
return controlname + " do not match.";
}
}
else
{
return controlname + " Cannot be Empty!.";
}
}
public string Isnumeric(string numeric)
{
Regex objnumeric = new Regex("^\\d+$");
if (!objnumeric.IsMatch(numeric))
{
// Minimum Two Characters.
return "Numeric Character only.";
}
else
{
return "";
}
}
public string Maxlength(string controlname,int maxlength, string controlvalue)
{
if (!String.IsNullOrEmpty(controlvalue))
{
if (controlvalue.Length!= maxlength)
{
return controlname + " Should not exceed the " + maxlength + " Character";
}
else
{
return "";
}
}
return "Please Enter " + controlname; ;
}
}
Control and label
<asp:TextBox ID="txtzipcode" runat="server"></asp:TextBox><asp:Label ID="lblzip"
runat="server"></asp:Label>
How To Use On Page
validate formvalidation = new validate();
lblzip.Text = formvalidation.IsZipCodeValid(txtzipcode.Text);
No comments:
Post a Comment