utkarsh

utkarsh

  • NA
  • 1
  • 0

javascript validation

Mar 1 2006 4:56 AM

 

 


From: Utkarsh Upadhyay [mailto:[email protected]]
Sent: Friday, February 24, 2006 1:08 PM
To: '[email protected]'
Subject: if attchment is not opening in prv mail copy this code in .js file

 

//Created By        :      Utkarsh Upadhyay         

//Created Date      :      02/02/06

//Description       :      Consist of Client-side validations

//Modified By       :      Utkarsh Upadhyay         

//Modified Date     :      13/02/06

 

function FP_preloadImgs() {//v1.0

 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();

 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }

}

 

function FP_swapImg() {//v1.0

 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;

 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;

 elm.$src=elm.src; elm.src=args[n+1]; } }

}

 

function FP_getObjectByID(id,o) {//v1.0

 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);

 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;

 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)

 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }

 f=o.forms; if(f) for(n=0; n<f.length; n++) {els=f[n].elements;

 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }

 return null;

}

 

//Browser check

//Used for compatibility with firefox.Gets the sibling of element

 function getPrvSibling(startBrother)

     {

        endBrother=startBrother.previousSibling;

        while(endBrother.nodeType!=1){

                endBrother = endBrother.previousSibling;

         }

        return endBrother;

    }

      

 //For Calender

 function callDate(txtId,txtIdPosition)

 {

  x=document.getElementById(txtId);

  x1=document.getElementById(txtIdPosition);

  popUpCalendar(x1,x,'dd/mm/yyyy');

 }     

      

      

//Not in Use: Backspace and delete do not work when using these functions.

//allows only alpahbets and space

/*function funNotNumbers(e)

   {

   var keynum;

   var keychar;

   var numcheck;

   if(window.event) // IE

    {

    keynum = e.keyCode;

    }

   else if(e.which) // Netscape/Firefox/Opera

    {

    keynum = e.which;

    }

   keychar = String.fromCharCode(keynum);

   numcheck = /^[A-Za-z ]/;

   return numcheck.test(keychar);

   }

  */

   //returns only numbers

/*   function funOnlyNumbers(e)

   {

   var keynum;

   var keychar;

   var numcheck;

   if(window.event) // IE

    {

    keynum = e.keyCode;

    }

   else if(e.which) // Netscape/Firefox/Opera

    {

    keynum = e.which;

    }

   keychar = String.fromCharCode(keynum);

   numcheck = /\d/;

   return numcheck.test(keychar);

   }

*/

 

//Allows only numbers and hyphen in the text field

//Used for validating BSB no in payment

 function funOnlyNumbersandHyphen(e)

   {

   var keynum;

   var keychar;

   var numcheck;

   if(window.event) // IE

    {

    keynum = e.keyCode;

    }

   else if(e.which) // Netscape/Firefox/Opera

    {

    keynum = e.which;

    }

   keychar = String.fromCharCode(keynum);

   numcheck = /[\d-]/;

   return numcheck.test(keychar);

   }

  

//Validates Email

//I/P param: Takes id of the control to validate.

 function funEmailValidation(id)

 {

  var emailPat = /^[A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)*[@][A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)+$/;

  if(document.getElementById(id).value==''|| document.getElementById(id).value ==null)

        return true;

  var objEmail = document.getElementById(id).value;

  var matchArray = objEmail.match(emailPat);

  if (matchArray == null)

  {

  // alert ("Enter valid email address.");

   document.getElementById(id).value="";

   document.getElementById(id).focus();

   return false;

  }

  return true;

 }

 

//Capitilise the first letter of the string provided as i/p

//I/p parameter: id of the control whose text has to be changed.

 function funCapitilise(id)

     {

        var tmpStr, tmpChar, preString, postString, stringLen ,i,loop;

        tmpStr = document.getElementById(id).value.toLowerCase();

        strLen=  tmpStr.length;

        if (strLen > 0)

        {

         for (j= 0; j < strLen; j++)

          {

            if (tmpStr.substring(0,1) ==" ")

            {  

              postString = tmpStr.substring(1,strLen);

              tmpStr = postString;  

            }

           }

         }

        stringLen = tmpStr.length;

        if (stringLen > 0)

        {

          for (i = 0; i < stringLen; i++)

          {

            if (i == 0 )

            {

              tmpChar = tmpStr.substring(0,1).toUpperCase();

              postString = tmpStr.substring(1,stringLen);

              tmpStr = tmpChar + postString;

            }

            else

            {

              tmpChar = tmpStr.substring(i,i+1);

              if (tmpChar == " " && i < (stringLen-1))

              {

              tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();

              preString = tmpStr.substring(0,i+1);

              postString = tmpStr.substring(i+2,stringLen);

              tmpStr = preString + tmpChar + postString;

              }

            }

          }

        }

        document.getElementById(id).value = tmpStr;

        return tmpStr;

    }

   

// Validates Date

//i/p parameter: Takes id of day,month and year control

function funDate(idDay,idMonth,idYear)

{

    var day = document.getElementById(idDay).value;

    var month = document.getElementById(idMonth).value;

    var year = document.getElementById(idYear).value;

    if (month < 1 || month > 12) { // check month range

         alert("Month must be between 1 and 12.");

         return false;

         }

    if (day < 1 || day > 31) {

         alert("Day must be between 1 and 31.");

         return false;

         }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {

         alert("Month "+month+" doesn't have 31 days!")

         document.getElementById(idMonth).value='1';

         return false;

         }

   if (month == 2)  // check for february 29th

         {

             var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

             if (day>29 || (day==29 && !isleap)) {

             alert("February " + year + " doesn't have " + day + " days!");

               document.getElementById(idMonth).value='1';

             return false;

          }

       }

    return true;

}

 

// Comapares date with the current date , if entered date is greater than today's date

// it returns false. i/p parameter: id of control,current year is passed from the server.

function funCurrentDate(objCurDate,currYear)

{

    var curDate=document.getElementById(objCurDate).value;

    var dt=curDate.split("/");

    var day = dt[0];

    var month = dt[1];

    var year1 = dt[2];

    var currentDate = new Date();

    var brwName = navigator.appName;

    if(year1 > currYear)

    {

       // alert("Year cannot be greater than current year");

        alert("Enter valid date.");

        return false;  

    }

    if(year1 < currYear)

    {

         return true;  

    }   

    if(year1 == currYear)

    {

        //   alert("Year cannot be greater than current year");

         //  return false;

        

           if(month > (currentDate.getMonth()+1))

           {

              alert("Month cannot be greater than current month");

              return false;

           }

           else

           {

              if(day > currentDate.getDate() && month == (currentDate.getMonth()+1))

              {

               alert("Date cannot be greater than today's date");

               return false;

              }

              else

              {

               return true;

              }

           }

     }

}

 

//Not in use.

function funCurrentDate1(strDate)

 {

  //  var ArrDt=document.getElementById(strDate).value;

   

  //  var day = ArrDt[0];

  //  var month = ArrDt[1];

  //  var year = ArrDt[2];

  //  var currentDate = new Date();

  //  var currentYear = currentDate.getYear();

 

//    if(year < currentYear)

//    {

     

 //       return true;

 ///   }

  //  if(year > currentYear)

  //  {

  //      alert("Year can not be greater than current year.")

  //      return false;   

 //   }

 //   if(year == currentYear)

 //   {

  //      //   alert("Year cannot be greater than current year");

  //       //  return false;

  //      

  //         if(month > (currentDate.getMonth()+1))

    //       {

  //            alert("Month cannot be greater than current month");

  //            document.getElementById(strDate).focus();

  //            return false;

  //         }

    //       else

    //       {

 //             if(day > currentDate.getDate() && month == (currentDate.getMonth()+1))

  //            {

 //              alert("Date cannot be greater than today's date");

  //             document.getElementById(strDate).focus();

 //              return false;

 //             }

//              else

 //             {

 //              return true;

 //             }

  //         }

 //    }

}

     

// Required field validator

// i/p parameter: array of controls which are mandatory and to be validated.

function funRequiredFields(arrControls)

{

   var len = arrControls.length;

   var arrReturnedControls = new Array();

  for (i=0;i<len;i++)

   {

      //For radio button

      if(document.getElementById(arrControls[i]).type == 'radio' && document.getElementById(arrControls[i+1]).type == 'radio' )

      {

         i=i+1;

         if(document.getElementById(arrControls[i]).checked==false &&  document.getElementById(arrControls[i-1]).checked==false)

         {

             document.getElementById(arrControls[i]).focus();

                if(document.getElementById(arrControls[i]).title != "XYZ")

                {

                var chd=document.createElement("ABC");

                chd.style.color="Red";

                var txt =document.createTextNode("*","nodeStar");

                chd.appendChild(txt);

                var brwName = navigator.appName;

                 if(brwName=="Microsoft Internet Explorer")

                 {

                     document.getElementById(arrControls[i]).parentNode.insertBefore(chd, document.getElementById(arrControls[i]).previousSibling);

                     document.getElementById(arrControls[i]).title="XYZ";

                 }

                 else

                 {

                        document.getElementById(arrControls[i]).parentNode.appendChild(chd);

                        document.getElementById(arrControls[i]).title="XYZ";

                  }

                }

             arrReturnedControls[arrReturnedControls.length] =arrControls[i];

         }

      }

      //For text box

      else if(document.getElementById(arrControls[i]).type == 'text')

      {

         var i , strLen,tempStr;

         tmpStr = document.getElementById(arrControls[i]).value.toLowerCase();

         strLen=  tmpStr.length;

         if (strLen > 0)

         { //Removes all the leading spaces

           for (j= 0; j < strLen; j++)

           {

             if (tmpStr.substring(0,1) ==" ")

             {  

                postString = tmpStr.substring(1,strLen);

                tmpStr = postString;  

             }

           }

         }

        

         if(document.getElementById(arrControls[i]).value=='' || document.getElementById(arrControls[i]).value.length <= 0 )

               {

                document.getElementById(arrControls[i]).focus();

                if(document.getElementById(arrControls[i]).title != "XYZ")

                {

                var chd=document.createElement("ABC");

                chd.style.color="Red";

                var txt =document.createTextNode("*","nodeStar");

                chd.appendChild(txt);

              

                 var brwName = navigator.appName;

                 if(brwName=="Microsoft Internet Explorer")

                 {

                     document.getElementById(arrControls[i]).parentNode.insertBefore(chd, document.getElementById(arrControls[i]).previousSibling);

                     document.getElementById(arrControls[i]).title="XYZ";

                 }

                 else

                 {

                        document.getElementById(arrControls[i]).parentNode.appendChild(chd);

                        document.getElementById(arrControls[i]).title="XYZ";

                  }

                }

             //Array of controls to be returned 

              arrReturnedControls[arrReturnedControls.length] =arrControls[i];

              }

       }

       //For checkbox

       else if(document.getElementById(arrControls[i]).type == 'checkbox')

       {

         if(document.getElementById(arrControls[i]).checked!=true)

         {

             document.getElementById(arrControls[i]).focus();

                if(document.getElementById(arrControls[i]).title != "XYZ")

                {

                var chd=document.createElement("ABC");

                chd.style.color="Red";

                var txt =document.createTextNode("*","nodeStar");

                chd.appendChild(txt);

                var brwName = navigator.appName;

                if(brwName=="Microsoft Internet Explorer")

                 {

                     document.getElementById(arrControls[i]).parentNode.insertBefore(chd, document.getElementById(arrControls[i]).previousSibling);

                     document.getElementById(arrControls[i]).title="XYZ";

                 }

                 else

                 {

                      document.getElementById(arrControls[i]).parentNode.appendChild(chd);

                      document.getElementById(arrControls[i]).title="XYZ";

                 }

                }

            arrReturnedControls[arrReturnedControls.length] =arrControls[i];

         }

       }

       else

       {

          //For rest of controls 

          if( document.getElementById(arrControls[i]).value=="Select.." || document.getElementById(arrControls[i]).value==""  || document.getElementById(arrControls[i]).value=="Years" ||  document.getElementById(arrControls[i]).value=="Months" ||document.getElementById(arrControls[i]).value=="Days" ||

               document.getElementById(arrControls[i]).value=="Year" ||  document.getElementById(arrControls[i]).value=="Month" ||document.getElementById(arrControls[i]).value=="Day")

                     {

                         document.getElementById(arrControls[i]).focus();

                        if(document.getElementById(arrControls[i]).title != "XYZ")

                        {

                        var chd=document.createElement("ABC");

                        chd.style.color="Red";

                        var txt =document.createTextNode("*","nodeStar");

                        chd.appendChild(txt);

                       

                 var brwName = navigator.appName;

                 if(brwName=="Microsoft Internet Explorer")

                 {

                     document.getElementById(arrControls[i]).parentNode.insertBefore(chd, document.getElementById(arrControls[i]).previousSibling);

                        document.getElementById(arrControls[i]).title="XYZ";

                 }

                 else

                 {

                        document.getElementById(arrControls[i]).parentNode.appendChild(chd);

                        document.getElementById(arrControls[i]).title="XYZ";        

 

                  }

               }

                      arrReturnedControls[arrReturnedControls.length] =arrControls[i];

              }

       }

      

     }

     if(arrReturnedControls.length != 0)

     document.getElementById(arrReturnedControls[0]).focus(); //Sets focus to the first control to be validated.

     return arrReturnedControls;    //Array of controls to be returned.

}

 

//Validates Phone no's

//At least two Ph no are mandatory.

function funValidateNoOfPhones()

    {

        var count;

        var objPh1 = document.getElementById("txtHomePh2").value;

        var objPh2 = document.getElementById("txtWorkPh2").value;

        var objPh3 = document.getElementById("txtMobNo").value;

        var objPh4 = document.getElementById("txtHomePh1").value;

        var objPh5 = document.getElementById("txtWorkPh1").value;

       

        if((objPh1 == '____ ____' && objPh4 != '__') || (objPh1 != '____ ____' && objPh4 == '__'))

            {

            alert("Enter valid phone number!");

            return;

            }

        if((objPh2 == '____ ____' && objPh5 != '__') ||(objPh2 != '____ ____' && objPh5 == '__'))

            {

            alert("Enter valid phone number!");

            return;

            }

        count=0;

        if (objPh1 != '____ ____' && objPh4 != '__')

            count= count +1;

         

        if (objPh2 != '____ ____'  && objPh5 != '__')

            count= count +1;

       

        if (objPh3 != '____ ___ ___')  

            count= count +1;

        if (count < 2)

         {

            alert("Enter atleast two Phone Numbers");

            document.getElementById("txtHomePh1").focus();

            return false;  

         }

         return true;

}

 

//Populates the Year combo

//i/p parameter: Id of year control

 function funPopulateYear(idYear)

    {

        var optn;

        var currentDate = new Date();

        var currentYear = currentDate.getYear();

       

        for(var iCount =currentYear ; iCount>=1900;iCount--)

        {

            optn = document.createElement("option");

            var txt = document.createTextNode(iCount);

            optn.appendChild(txt);

            optn.setAttribute("value",iCount);

            document.getElementById(idYear).appendChild(optn);

        }

   }

     

     

//This function is currently not in use.

function funPopulateYearforQuote4(idYear)

    {

        var optn;

        var currentDate = new Date();

        var currentYear = currentDate.getYear();

       

        for(var iCount =1988 ; iCount>=1900;iCount--)

        {

            optn = document.createElement("option");

            var txt = document.createTextNode(iCount);

            optn.appendChild(txt);

            optn.setAttribute("value",iCount);

            document.getElementById(idYear).appendChild(optn);

        }

     

        for(var iCount =currentYear ; iCount>1988;iCount--)

        {

            optn = document.createElement("option");

            var txt = document.createTextNode(iCount);

            optn.appendChild(txt);

            optn.setAttribute("value",iCount);

            document.getElementById(idYear).appendChild(optn);

        }

   }    

  

  

  

  

// Comapares date with the current date , if entered date is less than today's date

// it returns false. i/p parameter: id of controls and current Year. 

function funGCurrentDate(objcurDate,currentYear)

 {

    var curDate=document.getElementById(objcurDate).value;

    var dt=curDate.split("/");

  

    var day = dt[0];

    var month = dt[1];

    var year = dt[2];

    var currentDate = new Date();

    if(year < currentYear)

    {

        alert("Year cannot be less than current year");

        return false;  

    }

    if(year > currentYear)

    {

      return true;  

    }   

    if(year == currentYear)

    {

          if(month < (currentDate.getMonth()+1))

           {

              alert("Month cannot be less than current month");

              return false;

           }

           else

           {

              if(day < currentDate.getDate() && month <= (currentDate.getMonth()+1))

              {

               alert("Date cannot be less than today's date");

               return false;

              }

              else

              {

               return true;

              }

           }

     }

}

        

//Display Error Message        

function DisplayErrorMessage(result)

{

    var brwName = navigator.appName;

         if(brwName=="Microsoft Internet Explorer")

         {

           for (var iCount = 0 ; iCount<result.length;iCount++)

           {

              document.getElementById(result[iCount]).parentNode.previousSibling.style.color="Red";    

              document.getElementById("divError").style.display="block";

           }

            return false;

         }

         else

         {

           for (var iCount = 0 ; iCount<result.length;iCount++)

           {

                var objTd=getPrvSibling(document.getElementById(result[iCount]).parentNode);

                objTd.style.color="Red";   

                 document.getElementById("divError").style.display="block";

            }

            return false;

          }

}      

 

//returns only numbers

//Takes event as argument.

function funOnlyNumbers(evt)

{

    (evt)? evt:event;

    if(navigator.appName == "Microsoft Internet Explorer")

         var charCode = evt.keyCode;

    else

         var charCode = (evt.charCode)?evt.charCode:((evt.KeyCode)?evt.keyCode:((evt.which)?evt.which:0));

    if(charCode >32 && (charCode <48 || charCode>57))

    {

        return false;

    }

    return true;

}

 

//allows only alpahbets and space

//Takes event argument.

function funNotNumbers(evt)

{

    (evt)? evt:event;

    if(navigator.appName == "Microsoft Internet Explorer")

         var charCode = evt.keyCode;

    else

         var charCode = (evt.charCode)?evt.charCode:((evt.KeyCode)?evt.keyCode:((evt.which)?evt.which:0));

    if(charCode >32 && (charCode <65 || charCode >90)  && (charCode<97 || charCode>122))

    {

        return false;

    }

    return true;

}