Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

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;
  }

Friday, March 11, 2011

Java Script Validations

Check the Text field Value is Empty or not

function IsValueEmpty(ctrl) {
if (ctrl.value== '') {
        alert("This field cannot be empty!");
        return false;
 }
}

   

Check the selected index of the dropdown

function IsValueEmpty(ctrl) {
if (ctrl.selectedIndex == 0) {
        alert("Please select the one of the Item from the dropdown!");
        return false;
 }
}


Check the selected value of the checkbox

function Ischecked(ctrl) {
if (ctrl.checked == false) {
        alert("Please checked the value of the checkbox !");
        return false;
 }
}


Email Check

1)

function echeck(str) {

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

if (str != '') {

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

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

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

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

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

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

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



2)
function isEmail(argvalue) {
if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;
// arrayString = argvalue.split("@"); (works only in netscape3 and above.)
  var retSize = customSplit(argvalue, "@", "arrayString");
if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }
return true;
}


Num Check Function

1)
function NumbersOnlyWithEnter(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode < 48 || unicode > 57) //if not a number
    {
        if (unicode != 13) {
            return false //disable key press
        }
    }
}


2)
function numCheck(argvalue) {
if (argvalue.length == 0)
    return false;
for (var n = 0; n < argvalue.length; n++)
    if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
      return false;
return true;
}

Eg:
The followings will return "true".
numCheck("1234")
numCheck("0123")
The followings will return "false".
numCheck("abcd")
numCheck("a123")
numCheck("123a")
numCheck("12.3")



Check the URL format is Correct or Not
  
function isURL(argvalue) {
if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;
argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;
if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }
if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }
return true;
}