RSS

Some of the Basic Javascript user defind functions

To Identify of there is a any Item is available in a List box Using Javascript


function CheckSelected() {
var sMsg = "";
if (document.getElementById('<%=lstMinorErrors.ClientID %>') != null) {
//alert(document.getElementById('<%=lstMinorErrors.ClientID %>').options.length);
if (document.getElementById('<%=lstMinorErrors.ClientID %>').options.length < 0) {
sMsg = "No Items Found to Insert!"
}
}
if (sMsg != "") {
alert(sMsg);
return false;
}
else
return true;
}

To valid Gridview if any of the Checkbox item Selected or Not:

//To check whether the user checked any of the item in a Gridview for Delete Options
function ClientCheck(sMsg) {
var valid = false;
var gv = document.getElementById('<%=gvMinorErrorMaster.ClientID%>');//gvMinorErrorMaster- denotes gridview Name

for (var i = 0; i < gv.all.length; i++) {
var node = gv.all[i];
if (node != null && node.type == "checkbox" && node.checked) {
valid = true;
break;
}
}
if (!valid) {
alert("Please select a checkbox to continue.");
}
else {
var sConform = confirm(sMsg);
if (sConform == true)
valid = true;
else
valid = false;
}
return valid;
}


//In code behind of the page_load Event:
btnDelete.Attributes.Add("OnClick", "return ClientCheck('Are you sure to Delete the Selected Rows.');");

To check if any of the Items user select in a DropDown List

function CheckSearchProCustSel() {
var ddlReport;
var sObj;
var sErrMsg = "";
ddlReport = document.getElementById("<%=ddlSProCust.ClientID%>");//ddlSProCust- denotes Dropdown list Id
sObj = ddlReport.options[ddlReport.selectedIndex].value;//Get the Selected index value
//check if the selectedindex
if (sObj == 0) {
sErrMsg = "Select Customer-Project to Serach! \n";
}
if (sErrMsg != "") {
alert(sErrMsg);
return false;
}
else
return true;
}

To Trim the Spaces in a String from the textbox or other controls:

// Removes leading whitespaces
function LTrim(value) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim(value) {
return LTrim(RTrim(value));
}

0 comments:

Post a Comment