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

0 comments:

Post a Comment