class Validation {
Boolean ValiedOBject = false;
string str = "";
//Property Section
private string E_mail {
get {
return str;
}
set {
str = value;
Valied_Emails();
}
}
// End Property Section
private void Valied_Emails() {
specharacter1();
AllCaps();
}
private void specharacter1() {
string spl;
int i;
spl = "!@%^&*()+{[}]|\\;,?<>\'~` ";
for (i = 0;
(i <= (spl.Length - 1)); i++) {
if ((str.IndexOf(spl[i])) == 1) {
ValiedOBject = false;
return;
}
}
}
private void AllCaps() {
int i;
int l;
char[] str1;
str1 = str.Trim().ToCharArray();
l = str.Length;
for (i = 0;
(i <= (l - 1)); i++) {
if (((str1[i] >= Convert.ToChar("A")) && (str1[i] <= Convert.ToChar("Z")))) {
ValiedOBject = false;
return;
}
}
}
public string RemoveLastComa(string str) {
string Newstr = "";
int lstIndex = str.LastIndexOf(",");
Newstr = str.Remove(lstIndex, 1);
return Newstr;
}
public void FunSetFormat(System.Windows.Forms.TextBox txt) {
txt.BackColor = System.Drawing.Color.LavenderBlush;
txt.ForeColor = System.Drawing.Color.Navy;
if (!txt.Font.Bold == true) {
txt.Font = new System.Drawing.Font(txt.Font, System.Drawing.FontStyle.Bold);
}
}
public void FunUnsetFormat(System.Windows.Forms.TextBox txt) {
txt.BackColor = System.Drawing.Color.White;
txt.ForeColor = System.Drawing.Color.Black;
if (txt.Font.Bold == true) {
txt.Font = new System.Drawing.Font(txt.Font, System.Drawing.FontStyle.Regular);
}
}
public void SetFrmSize(System.Windows.Forms.Form frm) {
frm.Width = 742;
frm.Height = 697;
frm.Location = new System.Drawing.Point(0, 0);
}
public Boolean IsNumeric(object Expression) {
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
public void DateValid(TextBox mtbDate) {
//12/12/2009
string str = null;
string[] arr = null;
int pos = 0;
char splt = '/';
//mtbDate.Text = "12/12-2009"
if (mtbDate.TextLength != 10) {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
return;
} else {
pos = mtbDate.Text.Trim().IndexOf('/');
if (pos != 0 & pos == 2) {
pos = mtbDate.Text.Trim().IndexOf('/', pos + 1);
} else {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
return;
}
if (pos == 0) {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
return;
}
}
str = mtbDate.Text;
arr = str.Split(splt);
if (!string.IsNullOrEmpty(arr[0]) & !string.IsNullOrEmpty(arr[1]) & !string.IsNullOrEmpty(arr[2]) & IsNumeric(arr[0]) == true & IsNumeric(arr[1]) == true & IsNumeric(arr[2]) == true) {
if (Convert.ToInt16(arr[0]) <= 0 | Convert.ToInt16(arr[0]) > 31) {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
return;
} else if (Convert.ToInt16(arr[1]) <= 0 | Convert.ToInt16(arr[1]) > 12) {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
} else if (arr[2].Length != 4) {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
}
} else {
MessageBox.Show("Date is not Valid Please use DD/MM/YYYY format.");
mtbDate.Clear();
mtbDate.Focus();
return;
}
}In this code we will learn how to validate date,email and numeric values.
