/* File Name : Common.js Author : Kalyan Krishna. Date : 9/11/2000 Revisions : 20/11/2000 : 'fnIsVal' fixed 30/01/2000 : Kapil Commented out checking for first char alphanumeric in IsValidAlpha function. Purpose : Common JavaScript Validation code. */ ///////////////////////////////////////////////////////////////////////////////////////////// // General declarations ///////////////////////////////////////////////////////////////////////////////////////////// //function to append zeros to a number after decimal point to 2 places function fnRound(num1) { var inum1, inum2; inum2 = Math.round(num1); inum = ((inum2-num1)*100); //alert(inum); if (inum > 0) { if ((inum % 10) == 0) { return(num1 + "0"); } else { return(num1); } } else { return(num1 + ".00"); } } // function mouseover(){ event.srcElement.style.cursor="hand"; } // Function to remove whitespaces function fnTrim(varCtrl) { var Field Field = varCtrl if (!Field) // Validate the Form element { alert(" Function : Element is not Defined !"); return(-1); } var y=Field.value.length; var retval=Field.value; var m=0; while(m < y) //Start removing white spaces from left side { if ((retval.substring(0,1)==" ")||(retval.substring(0,1)==" ")) { retval=Field.value.substring(m+1,y); //remove left most white char } else { break; } m++; } y=retval.length; m=y; while (m >= 0) //Start removing white spaces from right side { if ((retval.substring(m-1,m)==" ")||(retval.substring(m-1,m)==" ")) { retval=retval.substring(0,m-1); //remove right most white char } else { break; //first non - white character encountered while //traversing from right to left. so break the loop } m--; } return retval; } // Checks wheather any given text box contains null values function fnIsTextNull(strMsg,varCtrl) { if (varCtrl.value == "") { var strErr; strErr = strMsg + " cannot be left blank."; alert(strErr); varCtrl.select(); varCtrl.focus(); return false; } return true; } // Function to check whether the field have all alphanumeric characters or not // Character Set that can come under Alphanumeric category are (A-Z), (a-z),. // Usage : fnIsValidAlpha("User Name",document.frmadm00c1.txtusername) function fnIsValidAlpha(strMsg,varCtrl) { var Field Field = varCtrl //if (!Field) // Validate the Form element //{ // alert (" Element is not Defined !"); // return(-1); //} Field.value = fnTrim(Field); // remove spaces from both side of the string/number var length = Field.value.length; //if (!fnIsCharacter(Field)) //{ // alert("First letter should be an alphabet in " + strMsg) // varCtrl.select(); // varCtrl.focus(); // return false; //} for (var ct = 0; ct <= (Field.value.length - 1); ct++) { var Validchar = Field.value.substring(ct,ct+1); // Check one Character at a time. var character = 0; // Set character as invalid alphanumeric value. if (((Validchar >= "A") && (Validchar <= "Z")) || ((Validchar >= "a") && (Validchar <= "z")) || ((Validchar >= "0") && (Validchar <= "9")) || (Validchar == ".") || (Validchar == " ") || (Validchar == "_") || (Validchar == "-")) { character = 1; //valid character } else { character = 0; //Invalid character alert("Enter a valid " + strMsg + " [Valid character set A-Z,a-z,.,_,-,0-9 ]"); varCtrl.select(); varCtrl.focus(); return false; } if (character == 0)// generate error message if not valid Character value. { alert("Must have valid characters in " + strMsg); varCtrl.select(); varCtrl.focus(); return false; //If not valid character } } return true; //If valid character } //Function to check whether the first charcter of field is alphabet // or not. // Return true if character is an alphabet. // Return true if character is not an alphabet. // Usage : fnIsCharacter(document.frmadm001c1.txtname) function fnIsCharacter(varCtrl) { var character var Validchar = varCtrl.value.substring(0,1) if (((Validchar >= "A") && (Validchar <= "Z")) || ((Validchar >= "a") && (Validchar <= "z"))) { return true // Valid character } else { return false //Invalid character } } function fnIsValidAlphaNumeric(strMsg,varCtrl) // Not in use. But good for reference. { var a,b var Field Field = varCtrl //if (!Field) // Validate the Form element //{ // alert (" Element is not Defined !"); // return(-1); //} Field.value = fnTrim(Field); // remove spaces from both side of the string/number var length = Field.value.length; if (!fnIsCharacter(Field)) { alert("First letter should be an alphabet in " + strMsg) return false; } a ="0"; for (var inCounter = 0; inCounter <= (Field.value.length - 1); inCounter++) { var Validchar = Field.value.substring(inCounter,inCounter+1); // Check one Character at a // time var character = 0; // Set character as invalid alphanumeric value if (((Validchar >= "A") && (Validchar <= "Z")) || ((Validchar >= "a") && (Validchar <= "z")) || ((Validchar >= "0") && (Validchar <= "9")) || (Validchar == "-") || (Validchar == "_") || (Validchar == ".")) { character = 1; //valid character b = a; a = Validchar; if ((a == b) && (a==".") && (b==".")) { alert("Please do not enter two dots (..) in " + strMsg); return false; } } else { character = 0; //Invalid character alert("Must have valid characters in " + strMsg); return false; } if (character == 0)// generate error message if not valid Character // value { alert("Must have valid characters in " + strMsg); return false; //If not valid character } } return true; //If valid character } // End of fnIsValidAlphaNumeric // Checks for existance of white spaces in any given string function fnCheckWhiteSpaces(strMesg,varCtrl) { var blnOk = true; var stItemValue = varCtrl.value; var inItemLength = stItemValue.length; //alert (inItemLength); //alert (stItemValue); for (i = 0; i<=inItemLength; i++) { if (stItemValue.charAt(i) == " ") { blnOk = false break; } else { continue; } } if (blnOk == false) { alert('The value entered for ' + strMesg + ' contains spaces.' + strMesg + ' cannot contain spaces.'); varCtrl.focus(); varCtrl.select(); return(false); } else{ return true; } } // Function to check the maximum and minimum number of characters for any field... function fnIsValidLength(strMsg,strVal,inMin,inMax) { stItemValue = strVal.value; inItemLength = stItemValue.length; // Validate the control's minimum value. if (inItemLength < inMin) { alert(strMsg + " cannot be of less than " + inMin + " characters.Please retry."); strVal.focus(); strVal.select(); return(false); } else if (inItemLength > inMax) // Validate the control's maximum value. { alert(strMsg + " cannot be of more than " + inMax + " characters.Please retry."); strVal.focus(); strVal.select(); return(false); } else { return(true); } } // Function to make sure that a control contains numbers only. function fnIsVal(stMsg,stVal2) { //alert("Here"); stVal = stVal2.value; //alert("Here"); var blnFlag var blnPrevFlag var stCtrlValue var inNum blnFlag=0 blnPrevFlag = 0 stCtrlValue = " " if (stVal.length > 0) { if (stVal != "0") { for (inNum=0;inNum 0) { if (stVal != "0") { for (inNum=0;inNum 0) { for (i = 0; i<=emaillength; i++) { if (str.charAt(i) == "@") { if((emaillength-i) < 4) { alert("There should be at least 2 characters after @."); varCtrl.focus(); varCtrl.select(); return(false); } flag=1; } if(flag==1) { if(str.charAt(i) == ".") { if((emaillength - i) < 2) { alert("There should at least be 2 characters after '.'"); varCtrl.focus(); varCtrl.select(); return(false); } flag=2; } } } if(flag!=2) { alert("Please enter your correct E-MailID"); varCtrl.focus(); varCtrl.select(); return(false); } } return(true); }