Wednesday, October 11, 2006

Detecting the browser using simple javascript function


Where the code used to check for if (ie) { ... }, now I wanted it to check for if (ie6OrLower) { ... }. So how to you tell the difference between IE 6 and IE 7+? You could parse the user-agent string, but I’d rather detect changes in the javascript object model. Here’s what I came up with:

if (typeof document.body.style.maxHeight != "undefined") {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}

This distinguishes between browsers based on the fact that IE 7 knows about the maxHeight css property, whereas previous versions of IE didn’t.

Tuesday, October 10, 2006

Simple javascript function to open all the collapsible panels


function OpenAll(sender)
{
if(sender == 'ctl00_CPH_CustomerCP')
{
var ids = ['ctl00_CPH_LiabilitiesCP','ctl00_CPH_IncomeCP'];
}

else if(sender == 'ctl00_CPH_LiabilitiesCP')
{
var ids = ['ctl00_CPH_CustomerCP','ctl00_CPH_IncomeCP'];
}

else if(sender == 'ctl00_CPH_IncomeCP')
{
var ids = ['ctl00_CPH_CustomerCP','ctl00_CPH_LiabilitiesCP'];
}

for (var i = 0; i < ids.length; i++)
{
var ctrl = $object(ids[i]);
if (!ctrl) continue;

// Musze sprawdzic wszystkie mozliwe zachowania
var behaviors = ctrl.get_behaviors();
for (var j = 0; j < behaviors.length; j++)
{
// Sprawdzam czy mamy CP o podanej nazwie
var behavior = behaviors[j];
if (Object.getTypeName(behavior) == 'AtlasControlToolkit.CollaspablePanelBehavior')
{
behavior._doOpen();
}
}
}
}

A simple javascript function to validate email address


This is a simple javascript funciton which validates the entered email address. You can add this to your javascript library functions. Know more information from this link

 

//calling the method
btnSubmit.Attributes.Add("OnClick", "javascript:return CheckEmail('txtEmail');")

//function CheckEmail
function CheckEmail(ControlID)
{
var str=document.getElementById(ControlID).value;
if (str != "" )
{

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)

if (str.indexOf(at)==-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}

if (str.indexOf(" ")!=-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
return true;
}
}