VivekMishra

Posts Tagged ‘Javascript functions

These all function are use when you use client side validation in your page .these are very  simple 
 
and use it with your javascript code
 
// function for checking string
function checkString(str_temp, str_fld)
{
      temp=new String(str_temp);
      if(str_temp=="")
      {
           window.alert("Please enter the " + str_fld);
           return false;
      }
 
      if(temp.charAt(0)==" ")
      {
           window.alert("The " + str_fld + " cannot start with a space");
           return false;
      }
 
      str=new String("'\"");
 
      var flag=1;
 
      for (c=0; c<temp.length; c++)
      {
           if(str.indexOf(temp.charAt(c))>=0)
           {
                 flag=0;
                 break;
           }
      }
 
      if (flag==0)
      {
           window.alert("The " + str_fld + " cannot contain single/double quotes");
           return false;
      }
      return true;
}
 
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
 
}
 
function IsZip(sText)
{
   var ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
 
}
 
// function for checking number
function checkNumber(str_temp, str_fld)
{
      temp=new String(str_temp);
      msg="";
      if(str_temp=="")
      {
           //window.alert("Please enter the " + str_fld);
           msg="Please enter the "+ str_fld;
           return msg;
      }
 
      if(temp.charAt(0)==" ")
      {
           //window.alert("The " + str_fld + " cannot start with a space");
           msg="Please enter the "+ str_fld + " cannot start with a space";
           return msg;
      }
 
      if (isNaN(str_temp))
      {
           //window.alert("The " + str_fld + " can contain only numbers");
           msg="Please enter the "+ str_fld + " can contain only numbers";
           return msg;
      }
      return msg;
}
 
// returns the filename(Thumbnail/Blowup name)
function funName(str_temp)
{
      temp=new String(str_temp);
 
      li=temp.lastIndexOf("\\");
      if(li!=-1)
      {
           li++;
           temp=temp.substr(li);
      }
      if(temp.indexOf(".")==-1)
      {
           return("");
      }
      return(temp);
}
//
function Trim(trimstr)
{
      if(trimstr.length < 1)
      {
           return"";
      }
      trimstr = RTrim(trimstr);
      trimstr = LTrim(trimstr);
      if(trimstr=="")
      {
           return "";
      }
      else
      {
           return trimstr;
      }
} //End Function
 
function RTrim(VALUE)
{
      var w_space = String.fromCharCode(32);
      var v_length = VALUE.length;
      var strTemp = "";
      if(v_length < 0)
      {
           return"";
      }
      var iTemp = v_length -1;
 
      while(iTemp > -1)
      {
           if(VALUE.charAt(iTemp) == w_space)
           {
           }
           else
           {
                 strTemp = VALUE.substring(0,iTemp +1);
                 break;
           }
           iTemp = iTemp-1;
      } //End While
      return strTemp;
} //End Function
 
function LTrim(VALUE)
{
      var w_space = String.fromCharCode(32);
      if(v_length < 1)
      {
           return"";
      }
      var v_length = VALUE.length;
      var strTemp = "";
 
      var iTemp = 0;
      while(iTemp < v_length)
      {
           if(VALUE.charAt(iTemp) == w_space)
           {
           }
           else
           {
                 strTemp = VALUE.substring(iTemp,v_length);
                 break;
           }
           iTemp = iTemp + 1;
      } //End While
      return strTemp;
} //End Function
function checkspecialcharacter(str)
{
      var iChars = "!@#$%^&*()+=[]\\\';,./{}|\":<>? ";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharfirstname(str)
{
      var iChars = "!@#$%^&*()+=[]\\;,./{}|\":<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharacterforname(str)
{
      var iChars = "!@#$%^&*()+=[]\\;/{}|\":<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checknumericcharacterforname(str)
{
      var iChars = "0123456789";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharacterforaddress(str)
{
      var iChars = "!@#$%^&*()+=[]\\\;./{}|\":<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharacterfortitle(str)
{
      var iChars = "!@#$%^&*()+=[]\\\;./{}|\"<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
 
function checkspecialcharacterfordesc(str)
{
      var iChars = "";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharacterforimage(str)
{
      var iChars = "!@#$%^&*()+=[]\';,/{}|\"<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
function checkspecialcharacterforUsername(str)
{
      var iChars = "!@^()+=[]\';,/{}|\"<>?";
      for (var i = 0; i < str.length; i++)
      {
           if (iChars.indexOf(str.charAt(i)) != -1)
           {
                 return false;
           }
      }
}
 
function isEmailAddr(email)
{
      var result = false
      var theStr = new String(email)
      var index = theStr.indexOf("@");
 
      if (index > 0)
      {
      var pindex = theStr.indexOf(".",index);
      if ((pindex > index+1) && (theStr.length > pindex+1))
           result = true;
      }
  return result;
}
 
function CheckEmail(Email)
{
      //var match = (/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/i);
 
      var match = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9_-]+\.)+[a-zA-Z0-9.-]{2,5}$/;
      if(match.test(Email)){return true}
      else
      {
           return false
      }
 
}
function emailfield(email)
{
      var msg="";
      if(!CheckEmail(email))
      {
           msg="Please enter a complete email address in the form: yourname@yourdomain.com!";
      }
      else if(Trim(email)=="")
      {
           msg="Email can not be blank!";
      }
      return msg;
}
function namefield(name)
{
      var msg="";
      if(checkspecialcharacterforname(name)==false)
      {
           msg="Name can not contain special character!"; //document.getElementById("divname").innerHTML
      }
      else if(Trim(name)=="")
      {
           msg="Name can not be blank!";
      }
      else if(checknumericcharacterforname(name)==false)
      {
           msg="Name can not contain numeric character!";
      }
      return msg;
}
function fnamefield(name)
{
      var msg="";
      if(checkspecialcharacterforname(name)==false)
      {
           msg="First name can not contain special character!"; //document.getElementById("divname").innerHTML
      }
      else if(Trim(name)=="")
      {
           msg="First name can not be blank!";
      }
      else if(checknumericcharacterforname(name)==false)
      {
           msg="First name can not contain numeric character!";
      }
      return msg;
}
function lnamefield(name)
{
      var msg="";
      if(checkspecialcharacterforname(name)==false)
      {
           msg="Last name  can not contain special character!"; //document.getElementById("divname").innerHTML
      }
      else if(Trim(name)=="")
      {
           msg="Last name can not be blank!";
      }
      else if(checknumericcharacterforname(name)==false)
      {
           msg="Last name can not contain numeric character!";
      }
      return msg;
}
function usernamefield(name)
{
      var msg="";
      if(checkspecialcharacterforUsername(name)==false)
      {
           msg="Username can not contain special character!";
      }
      else if(Trim(name)=="")
      {
           msg="Username can not be blank!";
      }
      else if(name.length < 5 )
      {
           msg="Please enter username in 5 character!";
      }
      return msg;
}
function passwordfield(password, confirmpassword)
{
      var msg="";
      if(checkspecialcharacterforUsername(password)==false)
      {
           msg="Password can not contain special character!";
      }
      else if(Trim(password)=="")
      {
           msg="Password can not be blank!";
      }
      else if(password.length < 5 )
      {
           msg="Please enter password in 5 character!";
      }
      else if(password!=confirmpassword)
      {
           msg="Your confirm password is not matched!";
      }
 
      return msg;
}
 
function titledfield(name,str)
{
      var msg="";
      if(checkspecialcharacterfortitle(name)==false)
      {
           msg=str + " can not contain special character!";
      }
      else if(Trim(name)=="")
      {
           msg=str + " can not be blank!";
      }
 
      return msg;
}
function titledfield2(name,str)
{
      var msg="";
      if(checkspecialcharacterfortitle(name)==false)
      {
           msg=str + " can not contain special character!";
      }
      else if(Trim(name)=="1234567654")
      {
           msg=str + " can not be blank!";
      }
 
      return msg;
}
 
function addressfield(name)
{
      var msg="";
      if(checkspecialcharacterforaddress(name)==false)
      {
           msg="Address can not contain special character!";
      }
      else if(Trim(name)=="")
      {
           msg="Address can not be blank!";
      }
      return msg;
}
function validateFileExtension(fvalue)
 {
      var msg="";
      if(fvalue!="")
      {
           if(!/(\.jpg|\.jpeg\.JPG|\.JPEG)$/i.test(fvalue))
            {
 
                 msg="Please select a JPG file";
 
           }
      }
      return msg;
}
function logofilefield(name)
{
      var msg="";
      if(name!="")
      {
           if (name.toUpperCase().indexOf(".JPG") < 0 && name.toUpperCase().indexOf(".JPEG") < 0)
           {
                 msg="Please select a JPG file";
           }
      }
      return msg;
}
function phonefield(phone, str)
{
      var msg="";
      var temp=phone;
      var stripped = temp.replace(/[\(\)\.\-\ ]/g, '');
      if (phone== "") {
        msg=str + " can not be blank!";
    }
      else if (!IsNumeric(stripped)) {
        msg=str + " can contain only hyphen and digits!";
    }
      else if(Trim(phone)=="")
      {
            msg=str + " can not be blank!";
      }
      else if((phone.length < 11 ) || (phone.length > 11 ))
      {
           msg="Please enter " + str + " Number in 11 digits!";
      }
 
      return msg;
}
 
function blankfieldnotcompulsory(name,str)
{
      var msg="";
      if (name!= "")
      {
           if(checkspecialcharacterfortitle(name)==false)
           {
                 msg=str + " can not contain special character!";
           }
           else if(Trim(name)=="")
           {
                 msg=str + " can not be blank!";
           }
      }
 
      return msg;
}
 
function phonefieldnotcompulsory(phone, str)
{
      var msg="";
      var temp=phone;
      var stripped = temp.replace(/[\(\)\.\-\ ]/g, '');
      if (phone!= "")
      {
      //  msg=str + " can not be blank!";
           if (!IsNumeric(stripped)) {
                 msg=str + " can contain only hyphen and digits!";
           }
           else if(Trim(phone)=="")
           {
                  msg=str + " can not be blank!";
           }
       }
      return msg;
}
 
function zipfield(zip, str)
{
      var msg="";
      var temp=zip;
      var stripped = temp.replace(/[\(\)\.\-\ ]/g, '');
      if (zip== "") {
        msg=str + " can not be blank!";
    }
      else if (!IsZip(stripped)) {
        msg=str + " can not contain special charaters !";
    }
      else if(Trim(zip)=="")
      {
            msg=str + " can not be blank!";
      }
      /*else if(zip.length>6)
      {
            msg=str + " no can contain only 6 digits!";
      }*/
 
      return msg;
}
 
function isWebSite(website)
{
      var result = false
      var theStr = new String(website)
      var index = theStr.indexOf(".");
 
      if (index > 0)
      {
      pin=theStr.length;
           if (pin > index+4){ result = true; }
      }
 
  return result;
}
 
function websitefield(website)
{
      var msg="";
      if (website== "") {
        msg="Website can not be blank!";
    }
      return msg;
}
 
function websitefieldnotcompulsary(website)
{
      var msg="";
      if (website!= "")
      {
        if (!isWebSite(website))
           {
                 msg="Invalid Website address ! ";
           }
    }
      return msg;
}
function tocheckblankfield(str, field)
{
      var msg="";
      if (str=="") {
        msg=field + " can not be blank!";
    }
      return msg;
}
function tocheckblankselection(str, field)
{
      var msg="";
      if (str=="") {
        msg=field + " can not be blank!";
    }
      return msg;
}
function tocheckblankselectionv(str, field)
{
      var msg="";
      if (str=="v") {
        msg=field + " can not be Please Select Option!";
    }
      return msg;
}
 
function descriptionfield(website)
{
      var msg="";
      if (website== "") {
        msg="Description can not be blank!";
    }
      return msg;
}
function tocheckfiletype(filetype,file)
{
      var msg="";
      var temp=filetype.toUpperCase();
      var temp1=file.toUpperCase();
      if ((temp1.indexOf(temp)) < 0 )
      {
           msg="Please select " + filetype + " file";
      }
      return msg;
}
 
function validateFiledocExtension(fvalue)
 {
      var msg="";
 
           if(!/(\.DOC|\.doc\.Doc)$/i.test(fvalue))
            {
 
                 msg="Please select a Doc extension file";
 
           }
 
      return msg;
}
 
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;
 
function isInteger(s){
      var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
 
function stripCharsInBag(s, bag){
      var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
 
function daysInFebruary (year){
      // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
      for (var i = 1; i <= n; i++) {
           this[i] = 31
           if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
           if (i==2) {this[i] = 29}
   }
   return this
}
 
function isDate(dtStr){
      dtStr=Trim(dtStr);
      var daysInMonth = DaysArray(12)
      var pos1=dtStr.indexOf(dtCh)
      var pos2=dtStr.indexOf(dtCh,pos1+1)
      var strYear=dtStr.substring(0,pos1)
      var strMonth=dtStr.substring(pos1+1,pos2)
      var strDay=dtStr.substring(pos2+1)
      strYr=strYear
      if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
      if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
      for (var i = 1; i <= 3; i++) {
           if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
      }
      month=parseInt(strMonth);
      day=parseInt(strDay);
      year=parseInt(strYr);
      var msg="";
      if (pos1==-1 || pos2==-1){
           msg="The date format should be : mm/dd/yyyy";
      }
      if (strMonth.length<1 || month<1 || month>12){
           msg="Please enter a valid month";
      }
      if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
           msg="Please enter a valid day";
      }
      if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
           msg="Please enter a valid 4 digit year between "+ minYear +" and "+ maxYear;
      }
      if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
           msg="Please enter a valid date";
      }
      return msg
}
 
function expfield(workexpx, workexpy)
{
var msg="";
//alert(workexpx);
//alert(workexpy);
if(parseInt(workexpx) > parseInt(workexpy))
{
msg="Please correct the minimum and maximum experiance value!";
}
 
return msg;
}
 
function validateFiledocExtensionnotcompulsory(fvalue)
{
var msg="";
if(fvalue!="")
{
if(!/(\.DOC|\.doc\.Doc)$/i.test(fvalue))
{
 
msg="Please select a word file(.doc extension)";
 
}
}
return msg;
}
function validateDOB(d,m,y){
var error = false;
//alert(d + '--' + m + '--' + y);
if (d == "" && m == "" && y == "") error = 'Date of birth can not be blank';
else if(d == "") error = 'Date can not be blank';
else if(m == "") error = 'Month can not be blank';
else if(y == "") error = 'Year can not be blank';
 
// Check for valid date
 
if(d > 28 && y != "")
{
if(m == 02)
{
febdays = daysInFebruary(y);
//if (parseInt(febdays) < 29)
if (d > parseInt(febdays) )
{
error = "invalid Date Of Birth";
}
 
}
else
{
if(d > 30)
{
if (m==4 || m==6 || m==9 || m==11)
{
error = "invalid Date Of Birth";
 
}
 
}
}
}
 
return error;
}

Advertisements


  • None
  • Mr WordPress: Hi, this is a comment.To delete a comment, just log in, and view the posts' comments, there you will have the option to edit or delete them.

Categories