Search This Blog

09 March 2011

Client side Validations

Client side validation runs in client's browser. For every request code is executed at server side and result is sent to the client in simple HTML format. Its performance is faster than server side validations.

// Create a class file and paste in App_code folder

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public class validation
{
public validation()
{

}
public bool Validate(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "fname")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.IndexOf('*') == 0 || ctls.Text.IndexOf('?') == 0 || ctls.Text.IndexOf('!') == 0 || ctls.Text.IndexOf('#') == 0 || ctls.Text.IndexOf(' {
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate1(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
if (str == "username")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 21)
{
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z'.\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
ctls.BackColor = System.Drawing.Color.White;
return true;
}
public bool Validate2(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "password")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if ((ctls.Text.Trim().Replace("'", "").Length <> 8))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z0-9\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
}
return true;
}
public bool Validate3(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "desc")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 500)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
private bool ValidateDate(string date)
{
try
{
// for US, alter to suit if splitting on hyphen, comma, etc.
string[] dateParts = date.Split('/');

// create new date from the parts; if this does not fail
// the method will return true and the date is valid
DateTime testDate = new
DateTime(Convert.ToInt32(dateParts[2]),
Convert.ToInt32(dateParts[0]),
Convert.ToInt32(dateParts[1]));

return true;
}
catch
{
// if a test date cannot be created, the
// method will return false
return false;
}
}

public bool Validate4(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "address")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 250)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
public bool validateDate(TextBox t1, TextBox t2)
{
bool temp=false;
if (Convert.ToDateTime(t1.Text).CompareTo(Convert.ToDateTime(t2.Text)) > 0)
{
temp=false;
}
else
{
temp = true;
}
return temp;
}

public bool Validate5(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "fname1")
{
if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 30)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.IndexOf('*') == 0 || ctls.Text.IndexOf('?') == 0 || ctls.Text.IndexOf('!') == 0 || ctls.Text.IndexOf('@') == 0 || ctls.Text.IndexOf('#') == 0 || ctls.Text.IndexOf(' {
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
public bool Validate6(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "pincode")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 6)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate7(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "phoneno")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length >10)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool dcheck(Control ctl)
{
DropDownList dl = new DropDownList();
dl = (DropDownList)ctl;
if (dl.SelectedIndex == 0)
{
return false;
}
else
{
return true;
}
}

public bool chkNumeric(Control ctl)
{
TextBox tx = new TextBox();
tx = (TextBox)ctl;
try
{
string i;
i = Convert.ToString(Convert.ToInt64(tx.Text));
}
catch (Exception e)
{
tx.BackColor = System.Drawing.Color.LightYellow;
return false;
}
return true;
}
public bool email(Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;

if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}

return true;
}

public bool ValidateConfirmPassword( Control Ctl, Control Ctlc)
{
TextBox ctls = new TextBox();
ctls = (TextBox)Ctl;
ctls.BackColor = System.Drawing.Color.White;
TextBox ctlsc = new TextBox();
ctlsc = (TextBox)Ctlc;
ctlsc.BackColor = System.Drawing.Color.White;


if (ctls.Text.Trim() != ctlsc.Text.Trim())
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}
if (ctlsc.Text.Trim().Length < 6)
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}

return true;
}
public bool ischeckBoxChecked(CheckBox c1, CheckBox c2, CheckBox c3)
{
if (c1.Checked == false && c2.Checked == false && c3.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermschkBxChecked(CheckBox c1)
{
if (c1.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermrdChecked(RadioButton rd)
{
if (rd.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool ValidateChar(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "url")
{
if (Regex.IsMatch(ctls.Text.Trim(), @"[0-9]"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
}) == 0 || ctls.Text.IndexOf('%') == 0 || ctls.Text.IndexOf('^') == 0 || ctls.Text.IndexOf('<') == 0 || ctls.Text.IndexOf('>') == 0)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate1(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
if (str == "username")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 21)
{
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z'.\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
ctls.BackColor = System.Drawing.Color.White;
return true;
}
public bool Validate2(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "password")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if ((ctls.Text.Trim().Replace("'", "").Length <> 8))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z0-9\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
}
return true;
}
public bool Validate3(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "desc")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 500)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
private bool ValidateDate(string date)
{
try
{
// for US, alter to suit if splitting on hyphen, comma, etc.
string[] dateParts = date.Split('/');

// create new date from the parts; if this does not fail
// the method will return true and the date is valid
DateTime testDate = new
DateTime(Convert.ToInt32(dateParts[2]),
Convert.ToInt32(dateParts[0]),
Convert.ToInt32(dateParts[1]));

return true;
}
catch
{
// if a test date cannot be created, the
// method will return false
return false;
}
}

public bool Validate4(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "address")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 250)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
public bool validateDate(TextBox t1, TextBox t2)
{
bool temp=false;
if (Convert.ToDateTime(t1.Text).CompareTo(Convert.ToDateTime(t2.Text)) > 0)
{
temp=false;
}
else
{
temp = true;
}
return temp;
}

public bool Validate5(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "fname1")
{
if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 30)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.IndexOf('*') == 0 || ctls.Text.IndexOf('?') == 0 || ctls.Text.IndexOf('!') == 0 || ctls.Text.IndexOf('@') == 0 || ctls.Text.IndexOf('#') == 0 || ctls.Text.IndexOf('$') == 0 || ctls.Text.IndexOf('%') == 0 || ctls.Text.IndexOf('^') == 0 || ctls.Text.IndexOf('<') == 0 || ctls.Text.IndexOf('>') == 0)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
public bool Validate6(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "pincode")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 6)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate7(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "phoneno")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length >10)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool dcheck(Control ctl)
{
DropDownList dl = new DropDownList();
dl = (DropDownList)ctl;
if (dl.SelectedIndex == 0)
{
return false;
}
else
{
return true;
}
}

public bool chkNumeric(Control ctl)
{
TextBox tx = new TextBox();
tx = (TextBox)ctl;
try
{
string i;
i = Convert.ToString(Convert.ToInt64(tx.Text));
}
catch (Exception e)
{
tx.BackColor = System.Drawing.Color.LightYellow;
return false;
}
return true;
}
public bool email(Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;

if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}

return true;
}

public bool ValidateConfirmPassword( Control Ctl, Control Ctlc)
{
TextBox ctls = new TextBox();
ctls = (TextBox)Ctl;
ctls.BackColor = System.Drawing.Color.White;
TextBox ctlsc = new TextBox();
ctlsc = (TextBox)Ctlc;
ctlsc.BackColor = System.Drawing.Color.White;


if (ctls.Text.Trim() != ctlsc.Text.Trim())
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}
if (ctlsc.Text.Trim().Length < 6)
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}

return true;
}
public bool ischeckBoxChecked(CheckBox c1, CheckBox c2, CheckBox c3)
{
if (c1.Checked == false && c2.Checked == false && c3.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermschkBxChecked(CheckBox c1)
{
if (c1.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermrdChecked(RadioButton rd)
{
if (rd.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool ValidateChar(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "url")
{
if (Regex.IsMatch(ctls.Text.Trim(), @"[0-9]"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
}) == 0 || ctls.Text.IndexOf('%') == 0 || ctls.Text.IndexOf('^') == 0 || ctls.Text.IndexOf('<') == 0 || ctls.Text.IndexOf('>') == 0)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
public bool Validate6(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "pincode")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 6)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate7(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "phoneno")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length >10)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool dcheck(Control ctl)
{
DropDownList dl = new DropDownList();
dl = (DropDownList)ctl;
if (dl.SelectedIndex == 0)
{
return false;
}
else
{
return true;
}
}

public bool chkNumeric(Control ctl)
{
TextBox tx = new TextBox();
tx = (TextBox)ctl;
try
{
string i;
i = Convert.ToString(Convert.ToInt64(tx.Text));
}
catch (Exception e)
{
tx.BackColor = System.Drawing.Color.LightYellow;
return false;
}
return true;
}
public bool email(Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;

if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}

return true;
}

public bool ValidateConfirmPassword( Control Ctl, Control Ctlc)
{
TextBox ctls = new TextBox();
ctls = (TextBox)Ctl;
ctls.BackColor = System.Drawing.Color.White;
TextBox ctlsc = new TextBox();
ctlsc = (TextBox)Ctlc;
ctlsc.BackColor = System.Drawing.Color.White;


if (ctls.Text.Trim() != ctlsc.Text.Trim())
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}
if (ctlsc.Text.Trim().Length < 6)
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}

return true;
}
public bool ischeckBoxChecked(CheckBox c1, CheckBox c2, CheckBox c3)
{
if (c1.Checked == false && c2.Checked == false && c3.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermschkBxChecked(CheckBox c1)
{
if (c1.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermrdChecked(RadioButton rd)
{
if (rd.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool ValidateChar(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "url")
{
if (Regex.IsMatch(ctls.Text.Trim(), @"[0-9]"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
}) == 0 || ctls.Text.IndexOf('%') == 0 || ctls.Text.IndexOf('^') == 0 || ctls.Text.IndexOf('<') == 0 || ctls.Text.IndexOf('>') == 0)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate1(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
if (str == "username")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 21)
{
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z'.\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
ctls.BackColor = System.Drawing.Color.White;
return true;
}
public bool Validate2(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "password")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if ((ctls.Text.Trim().Replace("'", "").Length <> 8))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^[a-zA-Z0-9\s]{1,50}$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
}
return true;
}
public bool Validate3(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "desc")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 500)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
private bool ValidateDate(string date)
{
try
{
// for US, alter to suit if splitting on hyphen, comma, etc.
string[] dateParts = date.Split('/');

// create new date from the parts; if this does not fail
// the method will return true and the date is valid
DateTime testDate = new
DateTime(Convert.ToInt32(dateParts[2]),
Convert.ToInt32(dateParts[0]),
Convert.ToInt32(dateParts[1]));

return true;
}
catch
{
// if a test date cannot be created, the
// method will return false
return false;
}
}

public bool Validate4(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "address")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
if (ctls.Text.Trim().Replace("'", "").Length > 250)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;

}
else
{

return true;
}
}
return true;
}
public bool validateDate(TextBox t1, TextBox t2)
{
bool temp=false;
if (Convert.ToDateTime(t1.Text).CompareTo(Convert.ToDateTime(t2.Text)) > 0)
{
temp=false;
}
else
{
temp = true;
}
return temp;
}

public bool Validate5(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "fname1")
{
if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 30)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.IndexOf('*') == 0 || ctls.Text.IndexOf('?') == 0 || ctls.Text.IndexOf('!') == 0 || ctls.Text.IndexOf('@') == 0 || ctls.Text.IndexOf('#') == 0 || ctls.Text.IndexOf('$') == 0 || ctls.Text.IndexOf('%') == 0 || ctls.Text.IndexOf('^') == 0 || ctls.Text.IndexOf('<') == 0 || ctls.Text.IndexOf('>') == 0)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
public bool Validate6(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "pincode")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length > 6)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool Validate7(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "phoneno")
{
if (string.IsNullOrEmpty(ctls.Text.Trim().Replace("'", "")))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (ctls.Text.Trim().Replace("'", "").Length >10)
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{
return true;
}
}
return true;
}
public bool dcheck(Control ctl)
{
DropDownList dl = new DropDownList();
dl = (DropDownList)ctl;
if (dl.SelectedIndex == 0)
{
return false;
}
else
{
return true;
}
}

public bool chkNumeric(Control ctl)
{
TextBox tx = new TextBox();
tx = (TextBox)ctl;
try
{
string i;
i = Convert.ToString(Convert.ToInt64(tx.Text));
}
catch (Exception e)
{
tx.BackColor = System.Drawing.Color.LightYellow;
return false;
}
return true;
}
public bool email(Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;

if (string.IsNullOrEmpty(ctls.Text.Trim()))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
if (!Regex.IsMatch(ctls.Text.Trim().Replace("'", ""), @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}

return true;
}

public bool ValidateConfirmPassword( Control Ctl, Control Ctlc)
{
TextBox ctls = new TextBox();
ctls = (TextBox)Ctl;
ctls.BackColor = System.Drawing.Color.White;
TextBox ctlsc = new TextBox();
ctlsc = (TextBox)Ctlc;
ctlsc.BackColor = System.Drawing.Color.White;


if (ctls.Text.Trim() != ctlsc.Text.Trim())
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}
if (ctlsc.Text.Trim().Length < 6)
{
ctlsc.BackColor = System.Drawing.Color.LightYellow;
ctlsc.Focus();
return false;

}

return true;
}
public bool ischeckBoxChecked(CheckBox c1, CheckBox c2, CheckBox c3)
{
if (c1.Checked == false && c2.Checked == false && c3.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermschkBxChecked(CheckBox c1)
{
if (c1.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool istermrdChecked(RadioButton rd)
{
if (rd.Checked == false)
{
return false;
}
else
{
return true;
}
}
public bool ValidateChar(string str, Control ctl)
{
TextBox ctls = new TextBox();
ctls = (TextBox)ctl;
ctls.BackColor = System.Drawing.Color.White;
if (str == "url")
{
if (Regex.IsMatch(ctls.Text.Trim(), @"[0-9]"))
{
ctls.BackColor = System.Drawing.Color.LightYellow;
ctls.Focus();
return false;
}
else
{

return true;
}
}
return true;
}
}

07 March 2011

Dynamic Creation of TextBoxes

// Create a method named createTextboxRow //

protected void createTextboxRow()
{
TextBox t;
int j;
j=Convert.ToInt32(TextBox1.Text);
for(int i=1;i<=j;i++)
{
t=new TextBox();
Form.Controls.Add(t);
}
}

// Calling of this method //

btn_Click(object sender,EventArgs e)
{
createTextboxRow();
}

Master Page

ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.

Creating Master Page

* Visual Studio 2008.
* Create new ASP.NET Web site, enter name as MasterPagesApp.
* Select Location as File System, and enter the path as D:\WhidbeyWebSites\MasterPagesApp( or any other path ).
* Select the Language you are more comfortable with, for this we will use C#.
* After creating the new Web Site, you will find a page created for you named Default.aspx.
* Delete this page and right click the application from solution explorer. Select Add New Item, select Master Page from installed Visual Studio templates and name it MainLayout, and check the Place code in separate file checkbox; this will create you a code-behind in the language you select, we work here using C#.

Master Pages
A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages. The directive looks like the following.
< %@ Master Language="C#" %>

The @ Master directive can contain most of the same directives that a @ Control directive can contain. For example, the following master-page directive includes the name of a code-behind file, and assigns a class name to the master page.
< %@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
In addition to the @ Master directive, the master page also contains all of the top-level HTML elements for a page, such as html, head, and form. For example, on a master page you might use an HTML table for the layout, an img element for your company logo, static text for the copyright notice, and server controls to create standard navigation for your site. You can use any HTML and any ASP.NET elements as part of your master page.

Content Pages
< %@ Page Language="C#" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page"%>
In the content page, you create the content by adding Content controls and mapping them to ContentPlaceHolder controls on the master page.

Advantages of Master Pages

Master pages provide functionality that developers have traditionally created by copying existing code, text, and control elements repeatedly; using framesets; using include files for common elements; using ASP.NET user controls; and so on. Advantages of master pages include the following:

* They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
* They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
* They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered.
* They provide an object model that allows you to customize the master page from individual content pages.

Run-time Behavior of Master Pages

At run time, master pages are handled in the following sequence:

* Users request a page by typing the URL of the content page.
* When the page is fetched, the @ Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled.
* The master page with the updated content is merged into the control tree of the content page.
* The content of individual Content controls is merged into the corresponding ContentPlaceHolder control in the master page.
* The resulting merged page is rendered to the browser.