var CURRENT_DATE=new Date('10/21/2017');function validateIntField(theField,paraName,isMandatory,minValue,maxValue,minDigitCount,maxDigitCount) { var ErrMsg =""; trimField(theField); var fieldValue = theField.value; if(fieldValue=="" && isMandatory) { ErrMsg = "Enter " + paraName + ".\n" } else if(fieldValue=="") { ErrMsg = ""; } else if(isNaN(fieldValue)) { ErrMsg = "The " + paraName + " you entered should be a numeric value.\n"; } else if(fieldValue.indexOf(".") != -1) { ErrMsg = "Entered value for " + paraName + " should not be a decimal value.\n"; } else if(GetInt(fieldValue) < 0) { ErrMsg = "Entered value for "+ paraName + " should be a positive value.\n"; } else if(minDigitCount != "" && !isNaN(minDigitCount) && fieldValue.length < minDigitCount) { ErrMsg = "The "+ paraName +" you enter should be a minimum of "+minDigitCount+" digits."; } else if(maxDigitCount != "" && !isNaN(maxDigitCount) && fieldValue.length > maxDigitCount) { ErrMsg = "The "+ paraName +" you entered should be less than or equal to "+maxDigitCount+" digits."; } else if( minValue != "" && !isNaN(minValue) && GetInt(fieldValue) < minValue ) { ErrMsg = "The " + paraName + " you entered should be greater than or equal to "+minValue+"."; } else if( maxValue != "" && !isNaN(maxValue) && GetInt(fieldValue) > maxValue ) { ErrMsg = "The " + paraName + " you entered should be less than or equal to "+maxValue+"."; } return ErrMsg; } function validateFloatField(theField,paraName,isMandatory,minValue,maxValue,precision) { var ErrMsg =""; trimField(theField); var fieldValue = theField.value; var portion = ""; if(fieldValue.indexOf(".") > 0) { portion =fieldValue.split('.')[1]; } if(fieldValue=="" && isMandatory) { ErrMsg = "Enter "+paraName+".\n" } else if(fieldValue=="") { ErrMsg = ""; } else if(isNaN(fieldValue)) { ErrMsg = "The " + paraName + " you entered should be a numeric value.\n"; } else if(GetFloat(fieldValue) < 0) { ErrMsg = "The "+ paraName + " you entered should be a positive value.\n"; } else if( precision != "" && !isNaN(precision) && portion.length > precision ) { ErrMsg = "The " + paraName + " you entered should have only "+precision+" digits after decimal point."; } else if( minValue != "" && !isNaN(minValue) && GetFloat(fieldValue) < minValue ) { ErrMsg = "The " + paraName + " you entered should be greater than or equal to "+minValue+"."; } else if( maxValue != "" && !isNaN(maxValue) && GetFloat(fieldValue) > maxValue ) { ErrMsg = "The " + paraName + " you entered should be less than or equal to "+maxValue+"."; } return ErrMsg; } function validateField(theField,checkType,minLimit,maxLimit,paraName,splCharString) { var ErrMsg=""; trimField(theField); var fieldValue = theField.value; //validation for not to be null if(fieldValue=="") { state=false; ErrMsg = "Enter "+paraName+".\n"; return ErrMsg; } //check for the type of value in the field if(checkType == "INT") { if(isNaN(fieldValue)) { ErrMsg = "Entered data in " + paraName + " field is not a numeric value.\n"; } if(fieldValue.indexOf(".") != -1) { ErrMsg = "Entered data for " + paraName + " should not be a decimal value.\n"; } if(GetInt(fieldValue) <= 0) { ErrMsg = paraName + " should be greater than 0"; } } else if(checkType == "FLOAT") { if(isNaN(fieldValue)) { ErrMsg = "The " + paraName + " you entered is not a numeric value.\n"; } if(GetFloat(fieldValue) <= 0) { ErrMsg = paraName + " should be greater than 0"; } } else if(checkType == "STRING") { //alert("enter the int zone "); } else if((checkType == "STRING-ONLY") && (!isNaN(fieldValue)) ) { ErrMsg = paraName + " should not be a number."; } else if(checkType == "NAME") { if(!isNaN(fieldValue)) { ErrMsg = paraName + " should not be a number."; } else { var firstchar = fieldValue.substring(0,1); if(!((firstchar >= 'a' && firstchar <= 'z') || (firstchar >= 'A' && firstchar <= 'Z'))) { ErrMsg="The first character in the "+paraName+" should be an alphabet."; } } } else if(checkType == "EMAIL") { //alert("enter the EMAIL zone "); if(! isEmailId(fieldValue)) { ErrMsg = "Enter a valid E-Mail address\n"; //ErrMsg +=" ex: \" prasad@hydbad.tcs.co.in \" "; } } else if(checkType == "IPADDRESS") { if(! isIpAddress(fieldValue)) { ErrMsg ="Enter valid IP address.\n"; } } else if(checkType == "WEBADDRESS") { if(fieldValue.indexOf(".") < 0) { ErrMsg ="Enter valid Web address."; } else { var arr = fieldValue.split('.'); if(arr.length < 3) ErrMsg ="Enter valid Web address."; for(i=0;i maxLimit ) { ErrMsg = "The " + paraName + " you entered should not be longer than "+maxLimit+" characters."; } if(fieldValue.length <= minLimit ) { ErrMsg = "The "+ paraName+" you entered must be longer than "+minLimit+" characters."; } if(checkSplCharsExist(fieldValue,splCharString)) { ErrMsg = "The " + paraName +" you entered should not have any special character."; } return ErrMsg; } function trimField(TheField) { if((TheField.type=='text') || (TheField.type=='textarea')) { var Strfldvalue=TheField.value; var Strtrmvalue=""; var j=0; for(k=0; k=0;k--) { if(fieldValue.charAt(k)==" " || fieldValue.charAt(k)==trimChar) { j--; } else { if(j > 0) { fieldValue = fieldValue.substring(0,j); } else { fieldValue = ""; } break; } } } return fieldValue; } function checkSplCharsExist(fieldValue, splCharString) { for(var i=0;i= 0) { return true; } } return false; } function GetInt(fieldValue) { var intValue = ""+trimValue(fieldValue,'0','LEFT'); if(intValue.l=='') { intValue ='0'; } return parseInt(intValue); } function GetFloat(fieldValue) { var floatValue = trimValue(fieldValue,'0','LEFT'); if(floatValue=='') { floatValue ='0'; } return parseFloat(floatValue); } function isEmailId(parEmailId) { var state=true; var count=0; var counta=0; var ErrMsg=""; //no successive two dots for(var i=0; i0) { diff--; } return diff; } /* This Function returns the Number of days in a month */ function getDaysInMonth(Intmonth,Intyear) { var Intdays=""; if (Intmonth==1 || Intmonth==3 || Intmonth==5 || Intmonth==7 || Intmonth==8 || Intmonth==10 || Intmonth==12) Intdays=31; else if (Intmonth==4 || Intmonth==6 || Intmonth==9 || Intmonth==11) Intdays=30; else if (Intmonth==2) { if (IsLeapYear(Intyear)) Intdays=29; else Intdays=28; } return (Intdays); } /* Check for leap year */ function IsLeapYear (IntYear) { if (((IntYear % 4)==0) && ((IntYear % 100)!=0) || ((IntYear % 400)==0)) { return (true); } else { return (false); } } /* (1) function to check whether the first date in the arguments is earlier // than second date */ function compareDate(Intfrom_day,Intfrom_month,Intfrom_year,Intto_day,Intto_month,Intto_year) { compare_date(Intfrom_day,Intfrom_month,Intfrom_year,Intto_day,Intto_month,Intto_year) } /*(2) function to check whether the first date in the arguments is earlier // than second date */ function compare_date(xIntfrom_day,xIntfrom_month,xIntfrom_year,xIntto_day,xIntto_month,xIntto_year) { var Isdate_earlier=true; var Intfrom_day = GetInt(xIntfrom_day); var Intfrom_month = GetInt(xIntfrom_month); var Intfrom_year = GetInt(xIntfrom_year); var Intto_day = GetInt(xIntto_day); var Intto_month = GetInt(xIntto_month); var Intto_year = GetInt(xIntto_year); if(Intfrom_year<=Intto_year) { if(Intfrom_year==Intto_year) { if(Intfrom_month<=Intto_month) { if(Intfrom_month==Intto_month) { if(Intfrom_day<=Intto_day) ; else Isdate_earlier=false; } else Isdate_earlier=true; } //if3 else Isdate_earlier=false; } //if2 else Isdate_earlier=true; } //if1 else Isdate_earlier=false; return(Isdate_earlier); } function isEqualDate(xIntfrom_day,xIntfrom_month,xIntfrom_year,xIntto_day,xIntto_month,xIntto_year) { var IsEqual = false; var Intfrom_day = GetInt(xIntfrom_day); var Intfrom_month = GetInt(xIntfrom_month); var Intfrom_year = GetInt(xIntfrom_year); var Intto_day = GetInt(xIntto_day); var Intto_month = GetInt(xIntto_month); var Intto_year = GetInt(xIntto_year); if(Intfrom_year==Intto_year && Intfrom_month == Intto_month && Intfrom_day == Intto_day) { IsEqual = true; } return IsEqual; } /* This function compares whether the given date is greater than or equal to the current date. Return value : TRUE -- if given date > current date FALSE -- if given date < current date */ function isPastDate(Intfrom_day,Intfrom_month,Intfrom_year) { today = CURRENT_DATE; var locDay = today.getDate(); var locMonth = today.getMonth(); var locYear = today.getYear(); //check if the browser is netscape, add 1900 to year if(navigator.appName.indexOf("Netscape") != -1) locYear = locYear+1900; locMonth++; locDay = GetInt(locDay,10); var isGreaterThan = compare_date(Intfrom_day,Intfrom_month,Intfrom_year,locDay,locMonth,locYear); if(isGreaterThan) return false; else return true; } /*(1-msg-compToCurrDate) This function checks whether the required option is selected in dropdown menu or not */ function validateDate(field,paraName,compToCurrDate) { var locDay=""; var locMonth=""; var locYear=""; var ErrMsg=""; var fieldValue = field.value; if(fieldValue=="") { ErrMsg = "Enter the " + paraName+".\n" return ErrMsg; } if(fieldValue.indexOf("/") < 0) { ErrMsg = "Enter valid " + paraName+"." ; return ErrMsg; } var arr = fieldValue.split("/"); if(arr.length != 3) { ErrMsg = "Enter valid " + paraName+"." ; return ErrMsg; } locDay = arr[0]; locMonth = arr[1]; locYear = arr[2]; for(i=0;i 2) ) { ErrMsg = "Enter valid " + paraName + "."; return ErrMsg; } } else if(i == 2) { if((arr[i].length == 0) || (arr[i].length != 4) ) { ErrMsg = "Enter " + paraName + " in valid format. Year value should be in 'yyyy' format."; return ErrMsg; } else if( GetInt(arr[i]) <1900) { ErrMsg = "Enter correct " + paraName + "."; return ErrMsg; } } } if(!isDate(locDay, locMonth, locYear)) { ErrMsg = "Enter valid " + paraName+"."; return ErrMsg; } if(compToCurrDate == "LESS") { if(isPastDate(locDay, locMonth, locYear)) ErrMsg +=paraName+" should be a date prior to the current date.\n"; } else if(compToCurrDate == "GREATER") { if(!isPastDate(locDay, locMonth, locYear)) ErrMsg +=paraName+" should be a date later than the current date.\n"; } else { // to be added... } return ErrMsg; } /* complete validation of a date field */ function isDate(Theday, Themonth, Theyear){ var stateday=false; var statemonth=false; var stateyear=false; var validDate=false; var daysgot=""; daysgot=getDaysInMonth(Themonth,Theyear); if(Theday <= daysgot) validDate=true; return(validDate); } function splitDate(val,delim) { var arr = val.split(delim); locDay = arr[0]; locMonth = arr[1]; locYear = arr[2]; return arr; } function checkDates(field1,field2,fieldName1,fieldName2,constraint) { var locDay1=""; var locMonth1=""; var locYear1=""; var locDay2=""; var locMonth2=""; var locYear2=""; var ErrMsg=""; var arr1 = splitDate(field1.value,"/"); var arr2 = splitDate(field2.value,"/"); var isLessThan = compare_date(arr1[0],arr1[1],arr1[2],arr2[0],arr2[1],arr2[2]); var isEqual = isEqualDate(arr1[0],arr1[1],arr1[2],arr2[0],arr2[1],arr2[2]); if(constraint == 'EQUAL') { if(!isEqual) { ErrMsg = fieldName1 + " should be same as " + fieldName2+"."; } } else if(constraint == 'LESS') { if(!isLessThan) { ErrMsg = fieldName1 + " should be prior to " + fieldName2+"." ; } } else if(constraint == 'GREATER') { if(isLessThan) { ErrMsg = fieldName1 + " should be later than " + fieldName2+"."; } } else if(constraint == 'LESS&EQUAL') { if(!isLessThan && !isEqual) { ErrMsg = fieldName1 + " should be earlier than or same as " + fieldName2+"." ; } } else if(constraint == 'GREATER&EQUAL') { if(isLessThan && !isEqual) { ErrMsg = fieldName1 + " should be later than or same as " + fieldName2+"."; } } return ErrMsg; } function checkEqual(field1, field2) { var arr1 = splitDate(field1.value,"/"); var arr2 = splitDate(field2.value,"/"); return isEqualDate(arr1[0],arr1[1],arr1[2],arr2[0],arr2[1],arr2[2]); } /* function validatePhone(stdField,phoneField) { var msg = ""; if(stdField.value != '' || phoneField.value != '') { msg = validateField(stdField,'INT','2','5','STD code',''); if(msg.length > 0) { alert(msg); stdField.focus(); return false; } if(stdField.value == 0) { msg = "Enter valid STD code"; alert(msg); stdField.focus(); return false; } if(stdField.value.charAt(0)=='0') { msg = "STD code should start with '0' "; alert(msg); stdField.focus(); return false; } msg = validateField(phoneField,'INT','4','7','Telephone No.',''); if(msg.length > 0) { alert(msg); phoneField.focus(); return false; } if(phoneField.value == 0) { msg = "Enter valid phone no"; alert(msg); phoneField.focus(); return false; } var length = stdField.value.length + phoneField.value.length; if(length < 10) { msg = "Complete phone no should not be less than 10 digits"; } else if(length >10) { msg = "Complete phone no should not be more than 10 digits"; } if(msg.length > 0) { alert(msg); stdField.focus(); return false; } } return true; } */ function validatePincode(pincodeField,paraName,mandatory) { var msg = ""; if(mandatory == true || pincodeField.value.length>0) { msg = validateField(pincodeField,'INT','','',paraName,''); if(msg=="" && pincodeField.value.length != 6) { msg = paraName + " should be six digits."; } var startDigit = pincodeField.value.substring(0,1); if( msg=="" && startDigit!="5") { msg = "The " + paraName + " for Andhra Pradesh starts with 5."; } } return msg; }