menu

Sunday, June 27, 2010

Password field validation (match) through javascript

Write this javascript code inside "HEAD" - "SCRIPT" tag

function chkPass()
{
if(form1.TextBox1.value.length < 5)
{
alert("Password must be of more than 5 and less char");
return (false);
}
else if(form1.TextBox1.value != form1.TextBox2.value)
{
alert("Both Password are not the same");
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return chkPass()"

Saturday, June 26, 2010

Textbox validation for special characters

Write this javascript code inside "HEAD" -> "SCRIPT" tag

function validtForSpclChar()
{
var chkStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var chkTxt = form1.TextBox1.value;
var allValid = true;
for (i = 0; i <>
{
ch = chkTxt.charAt(i);
for (j = 0; j <>
if (ch == chkStr.charAt(j))
break;
if (j == chkStr.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Enter only letter and numeric characters in the \"TextBox\" field.");
form1.TextBox1.focus();
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return validtForSpclChar(this)"

Required text field validator using javascript

Write this javascript code inside to "HEAD" tag of .aspx file

function FormValidator(theForm)
{
if (theForm.TextBox1.value == "")
{
alert("You must enter somthing");
theForm.TextBox1.focus();
return (false);
}
}

call this function in form's onsubmit event

form id="form1" runat="server" onsubmit="return FormValidator(this)"

Tuesday, June 22, 2010

How to check if a particular session is alive

Suppose we have a login page and a home page, if login is success redirect to home page. And if session is expire on home page it again redirects to login page.

code in login.aspx.cs page

protected void Page_Load(object sender, EventArgs e)
{
Session["sessionid"] = Session.SessionID;
}

code in web.config file

--
--
--
--

where timeout="20" is in minutes.

code in home.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (Session["sessionid"]==null)
{
Response.Redirect("login.aspx");
}
}