A Technical Resource for Microsoft .NET & Oracle
(Architect @IBM India Pvt, Ltd)
Thursday, May 6, 2010
JavaScript: Generic functions
function toggle(control)
{
var element = document.getElementById(control);
if (element.style.display == "none" )
element.style.display = "";
else
element.style.display = "none";
};
function setdisplay(control, display)
{
var element1 = document.getElementById(control);
alert(element1.name);
if (display == "true" )
{
element1.style.display = "";
}
else
{
element1.style.display = "none";
}
};
function uppercase()
{
key = window.event.keyCode;
if (key == 13)
return
false; //Ignore the enter key
if ((key > 0x60) && (key < 0x7B))
window.event.keyCode = key-0x20;
};
function DisableEnterKey()
{
key = window.event.keyCode;
if (key == 13)
event.returnValue = false; //Ignore the enter key
};
function uppercase1()
{
key = window.event.keyCode;
if ((key > 0x60) && (key < 0x7B))
window.event.keyCode = key-0x20;
}
function setdisplaycheck(control1, chkbox) {
var element1 = document.getElementById(control1);
var chkbox = document.getElementById(chkbox);
alert(element1.id);
if (chkbox.checked == "true" )
{
element1.style.display = "";
}
else
{
element1.style.display = "none";
}
};
function setvisibility(control, visiblity)
{
var element = document.getElementById(control);
element.visible == visiblity;
};
function setvisibilitytoggle(control) {
var element = document.getElementById(control);
if (element.visible == "true" )
element.visible = "false";
else
element.visible = "true";
};
/*
<input name="number" onKeyPress="return numbersonly(event, false)"/>
*/
function numbersonly(e, AcceptDecimals) {
var key;
var keychar;
if (window.event)
{
key = window.event.keyCode;
}
else
if (e)
{
key = e.which;
}
else
{
return
true;
}
if (key == 13)
return
false; //Ignore the enter key
keychar = String.fromCharCode(key);
if (isNaN(keychar))
if ((AcceptDecimals == 'true') && (keychar == '.'))
return
true;
else
return
false;
else
return
true;
};
/*
<input type=text name=en onKeyup="isInteger(this.value)"> */
function IsInteger(s)
{
var i;
s = s.toString();
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (isNaN(c))
{
alert("Given value is not a number");
return
false;
}
}
return
true;
};
/*
<input type="text" onkeypress="return onlyNumbers();">
*/
function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode == 13)
return
false; //Ignore Enter Key
else
if (charCode > 31 && (charCode < 48 || charCode > 57))
return
false; //Ignore if it is not a number
else
return
true;
};
/*
Function for displaying a popup NON-Model window
*/
function openWindow(url,pLeft, pTop)
{
var _Left ;
var _Top ;
var _Height = '230';
var _Width = '250';
if (pLeft === undefined )
_Left = '300';
else
_Left = pLeft;
if (pTop === undefined )
_Top = '310';
else
_Top = pTop;
window.open(url, 'OpenerWindow', 'height=' + _Height + ',width=' + _Width + ',left=' + _Left + ',top=' + _Top + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
};
function SetWaitCursor()
{
document.body.style.cursor = 'wait';
return
true;
} ;
function SetDefaultCursor()
{
document.body.style.cursor = 'default';
} ;
function OpenMe()
{
window.moveTo(0,0);
if (document.all)
{
top.window.resizeTo(screen.availWidth,screen.availHeight);
}
else
if (document.layers||document.getElementById)
{
if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth)
{
top.window.outerHeight = screen.availHeight;
top.window.outerWidth = screen.availWidth;
}
}
}
function IsEmpty(control,displayalert)
{
alert(control);
var element = document.getElementById(control);
alert(element.value);
if (element.value == null )
{
if(displayalert)
{
alert("Please select the date");
return
false;
}
}
else
return
true;
};
function isDate (value)
{
//return (!isNaN (new Date (value).getYear () ) ) ;
try
{
if (isNaN(new Date (value).getYear()))
{
alert("Please select the valid date");
return
false ;
}
else
return
true;
}
catch (ex)
{
alert("Please select the valid date - Error : " + ex.message );
return
false ;
}
}
function myIsDate(mystring)
{
var mystring, myresult ;
var mystring = new Date(mystring);
isNaN(mystring)? myresult=false : myresult=true ;
if (myresult == false )
{
alert("Please select the valid date");
return
false ;
}
else
return
true;
}
function ClearTextBoxs()
{
var loTextBox = document.forms[0].elements;
for(var i=0;i <= loTextBox.length ; i++ )
{ if(loTextBox[i].tagName =="input")
loTextBox[i].value = "";
}
}
function ConvertNullToEmptyString(strinput)
{
return (strinput == null ? "" : strinput);
}
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
function setSelectedIndex(s, v)
{
var bFound = false;
for ( var i = 0; i < s.options.length; i++ )
{
if ( s.options[i].value == v )
{
s.options[i].selected = true;
bFound = true;
return;
}
}
//If Item not found then set the selected index to 0
if (bFound == false)
s.options[0].selected = true;
}
function setSelectedIndexByText(s, v)
{
var bFound = false;
for ( var i = 0; i < s.options.length; i++ )
{
if ( s.options[i].text == v )
{
s.options[i].selected = true;
bFound = true;
return;
}
}
//If Item not found then set the selected index to 0
if (bFound == false)
s.options[0].selected = true;
}
No comments:
Post a Comment