Custom Function‎ > ‎

Common Java Script Functions



/* To register a method to be called on load of window (Web Form) */

              

function addLoadEvent(func) {

               var oldonload = window.onload;

               if (typeof window.onload != 'function') {

                               window.onload = func;

               }

               else {

                               window.onload = function() {

                                              oldonload();

                                              func();

                               }

               }

}

   

/*Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable to call AJAX */

 

function CreateXmlHttp()

{

            //Creating object of XMLHTTP in IE

            try

            {

                        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

            }

            catch(e)

            {

                        try

                        {

                                    XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

                       

                        }

                        catch(oc)

                        {

                                    XmlHttp = null;

                        }

            }

            //Creating object of XMLHTTP in Mozilla and Safari

            if(!XmlHttp && typeof XMLHttpRequest != "undefined")

            {

                        XmlHttp = new XMLHttpRequest();

            }

}

 

/* Commonly used Ajax method */

 

function ajaxFunc()

{   

    CreateXmlHttp();

    XmlHttp.onreadystatechange = HandleResponseS;                     

    XmlHttp.Open("POST",URL, false);

    XmlHttp.send();  

   

    return XmlHttp.responseText;

 

}

   

/* To check current Check Box and uncheck all other (may be in a Grid) */             

 

function CheckUncheck(chk)  // where chk is the object of checkbox (normally in a Grid)

{

    var chkid = chk.id;

   var ValChecked = chk.checked;  

  

    var frm = document.forms[0];

  // Loop through all elements

  

    for (i = 0; i < frm.length;i++)

    { 

      //alert(frm.elements[i].id);

      if(frm.elements[i].id.indexOf('chk') !=  - 1)

      {

        

          if(ValChecked  && frm.elements[i].id != chkid)

          {

                frm.elements[i].checked = false; 

          }

      }

    }

}

   

/* To open any Window in popup */

 

function openWin(url,title,w,h) // where w is width and h is the height of new popup window

{

    if(w != null && h != null)

        myWin = window.open(url,title,"height="+h+",width="+w+",scrollbars=yes,status=yes,toolbar=no,menubar=no,location=center");

    else

        myWin = window.open(url,title,"scrollbars=yes,status=yes,toolbar=no,menubar=no,location=center");

//  myWin.moveTo(10,10);

    myWin.focus();

}

   

/* Decimal check */                          

/* normally called as a part of validation of form for eg. may on Submit button Click */

 

function CheckDecimal(obj,NumberofDigitsAfterDecimal) // where obj is the object to validate

{

  var numberValue=document.getElementById(ClientID+obj).value;

  if(numberValue =="." )

  {

   document.getElementById(ClientID+obj).value='0.';

   return true;

  }

   if(numberValue ==" " )

   {

     document.getElementById(ClientID+obj).value='';

     return false;

   }

   else

   {

     if(isNaN(numberValue))

     {

        alert("Invalid Decimal Number");

        document.getElementById(ClientID+obj).value='';

         return false;

     }

     else

    {

        if(numberValue.indexOf('E') > -1)

        {

          alert("Invalid Decimal Number");

          document.getElementById(ClientID+obj).value='';

           return false;

        }

        if(numberValue.indexOf('.') > -1)

        {

             if(numberValue.length - (numberValue.indexOf('.')+1) > NumberofDigitsAfterDecimal)

             {

                var arr=new Array();

                arr=numberValue.split('.');

                var temp=arr[1];

                var newVal =arr[0]+"."+temp.substring(0,NumberofDigitsAfterDecimal)

                document.getElementById(ClientID+obj).value='';

                alert("Only " +    NumberofDigitsAfterDecimal + " digits are allowed after decimal.");

                return false;

             }

             else

             {

              

                  return true;

             }

        }

        else

        {

              if(parseDouble(numberValue) > 0)

              {

                   

                    return true;

              }

              else

              {

                    alert("Invalid Decimal Number");

                    document.getElementById(ClientID+obj).value='';

                  return false;

              }      

        }

    }

  }

}

   

/* Number Only Check */

/* called on Key press event of object on which the validation is applied */

 

function checkNumberOnly(e)  // where e stands for the event.

{        

    

      var key = "";

   

        if (navigator.appName == "Mozilla" || navigator.appName == "Netscape")

        {

           

            key = e.charCode;      

        }

        else

        {

            key = window.event.keyCode;      

        }

     

          if(key < 48 || key > 57) //checking B/W 0---9

          {

              if(key!=8 && key!=127 && key!=0)

              {

                e.returnValue = null;                  

                return false;                    

              }

              else

              {

                return true; 

              }

          }

          else

          {

                return true;

          }       

 

}

   

/* To Round a numeric value for eg. roundval(5.45,1) => 5.5 */ 

 

function roundval(val, sc)  // where val => value and sc => scale (decimal places)

{

    var scale = parseInt(sc);

    var temp = val.toString();

    var index = temp.indexOf('.');

   

    if(index != -1)

    {

        var len = temp.length;

        var man = temp.substring(0,index);

        var exp = temp.substring(index+1, len);

        if(exp.length > scale)

        {

            while(exp.length != scale)

            {

                var last_val = parseInt(exp.charAt(exp.length - 1));

                var second_last_val = parseInt(exp.charAt(exp.length - 2));

           

                if(last_val > 4)

                    second_last_val += 1;

               

                exp = exp.substring(0,exp.length - 2);

                exp += second_last_val.toString();

            }

   

            var temp = man.toString() + "." + exp.toString();

        }

    }

        return parseFloat(temp);

}

   

/* Date Validation */

/* DDMMYYYY Format*/

 

function validateDate_DDMMYYYY(dateValue)  // where dateValue => string with date

   var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
  
    if ((dateValue.match(RegExPattern)) && (dateValue!=''))
     {
        return true;
     }
     else
     {
     return false;
     }
}

 

/* MMDDYYYY Format*/


function validateDate_MMDDYYYY(dateValue)  // where dateValue => string with date

{
     var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
     if ((dateValue.match(RegExPattern)) && (dateValue!=''))
     {
        return true;
     }
     else
     {
     return false;
     }
}

 

 

/* To Trim a string in Java Script */


function trim(stringToTrim) {
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
 return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
 return stringToTrim.replace(/\s+$/,"");
}

 

/* Alternate Approach – it adds trim as an attribute to String */

 

String.prototype.trim = function() {
 return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
 return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
 return this.replace(/\s+$/,"");
}

 

       

Help Links:

 
Comments