Friday, March 18, 2011

User Define Functions In Java Script

Here are some functions which are very important in developments.


To Trim the String type text

function Trim(str) {
    return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);
return rtrim(tmpstr);
}


Check the Text field no of characters

function NoOfCharacters(ctrl) {
return  ctrl.value.length;
}


To Get the Part of a String

Function Substring(str,startValue,endValue) {
return  ctrl.substring(startValue,endValue);
}

Eg:
Substring('ABCDE',0,1)
 'A'


To Split a Text

1)
function Splitting(str,SplitBy) {
        var arrSplit =str.split(SplitBy);
        var intCount = 0;
        var strRetVal = '';
        while (intCount < arrSplit.length) {
            strRetVal += arrSplit[intCount] + ' - ' + '';
            intCount += 1;
        }
return strRetVal ;
}

Eg:
 Splitting('ABC\nDEFG','\n')  
 'ABC-DEFG'


2)
function customSplit(strvalue, separator, arrayName) {
  var n = 0;
if (separator.length != 0) {
    while (strvalue.indexOf(separator) != -1) {
      eval("arr"+n+" = strvalue.substring(0, strvalue.indexOf(separator));");
      strvalue = strvalue.substring(strvalue.indexOf(separator)+separator.length,
          strvalue.length+1);
      n++;
    }
    eval("arr" + n + " = strvalue;");
    arraySize = n+1;
  }
  else {
    for (var x = 0; x < strvalue.length; x++) {
      eval("arr"+n+" = \"" + strvalue.substring(x, x+1) + "\";");
      n++;
    }
    arraySize = n;
  }
eval(arrayName + " = new makeArray(arraySize);");
for (var i = 0; i < arraySize; i++)
    eval(arrayName + "[" + i + "] = arr" + i + ";");
return arraySize;
}


Eg:
var strvalue = "abc##123##zzz##$$$";
var returnArraySize = customSplit(strvalue, "##", "NewArray");
The above will create the following:
NewArray[0] has value "abc"
NewArray[1] has value "123"
NewArray[2] has value "zzz"
NewArray[3] has value "$$$"
returnArraySize      has value "4"



Replace Text

function ReplaceString(str,oldValue,newValue)
{
Return  str.replace(oldValue,newValue);
}

Eg:
ReplaceString('Hello World','World','!')
Hello !


Sort Method
//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]

//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]



Custom Sort Method

function customSort(a,b) {
    return( a.toString().length - b.toString().length );
}

var arlene = new Array("orange","apple","strawberry","banana");
alert( arlene.sort(customSort).toString() );


Add Space

function addSpace(argvalue, numlength) {
if (! numlength > 0)
    numlength = 10;
if (argvalue.length < numlength) {
    for(var i = argvalue.length; i < numlength; i++)
      argvalue = " " + argvalue;
  }
return argvalue;
}

Eg:
addSpace("123.45", 10);
"    123.45"


Highlighted the text in selected textBox

function ShowSelection(textComponent) {
    var selectedText;
    // IE version
    if (document.selection != undefined) {
        textComponent.focus();
        var sel = document.selection.createRange();
        selectedText = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos)
    }
    return selectedText;
}


Disable the Key Press event in a text box

function DisableKeyPressEvent(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode!=0) //if not a number
    {
        return false //disable key press
    }
}


Select the Item from ComboBox

function SelectComboItem(combo,value)
    {
    if(value!=null)
        {
          for (var i=0 ; i <= combo.length; i++)
            {
            combo.selectedIndex= i;
            if (combo[combo.selectedIndex].value==value)
              {
               combo.selectedIndex= i;
               return true;
              }
             else
              {
              combo.selectedIndex= 0;
              } 
            }
        }
    else
        {
            combo.selectedIndex= 0;
        }
    }



Set Current Date

function setDate()
  {
 
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
 
var dateLast=day+'/'+month+'/'+year;


   document.getElementById("txtDate").value=dateLast;

  return true;
  }

0 comments:

Post a Comment