/* 1. Removing spaces form both sides. checkTrim(txtString) 2. Validation for field which should not be empty isEmpty(fieldname,fieldvalue) Validation for field which should not be selected isSelected(fieldname,fieldvalue) 3. Validation for field which can contain only alphanumeric value hasOnlyAlphaNumeric(fieldname , fieldvalue) hasOnlyAlphaNumericwithDot(fieldname , fieldvalue) hasOnlyAlphaNumericWithSymbol(fieldname , fieldvalue) isValidString(fieldname , fieldvalue) 4. Validation for field which cannot contain Space inbetween. isSpace(fieldname, fieldvalue) 5. Validation for field which cannot start with number isStartsWithNumber(fieldname , fieldvalue) 6. Validation for field which can allow only alphabets hasOnlyAlphabets(fieldname ,fieldvalue) hasOnlyAlphabetsAndSpecificChar(fieldname , fieldvalue) 7. Validation for field which allows only numbers hasOnlyNumeric(fieldname , fieldvalue) hasOnlyNumericAndSpecificChar(fieldname , fieldvalue) hasOnlyNumericAndComma(fieldname , fieldvalue) 8. Validation for length of the field isTooLong(fieldName,checkStr,length) isTooShort(fieldName,checkStr,length) 9. Check for Valid Email. validateEmail(fieldname, fieldvalue) 10. Validation for two field to be same isDuplicate(firstValue, secondValue) 11. returns true if it is a valid phone Number isValidPhoneNO(fieldname , fieldvalue) validatePhone(fieldname, frmField) check_usphone(phonenumber, useareacode) 12. Validate Date validateSingleDate(dtDate) validateDate(startDate,endDate) isDateBefore(date1Name,date1Value,date2Name,date2Value) isDateAfter(date1Name,date1Value,date2Name,date2Value) 13. Validation for field which cannot contains symebol isValidString(fieldname , fieldvalue) 14. Validation for field which contains file name isValidFileName(fieldname , fieldvalue)] 15. Validation for field which allows only float numbers - allow also negative values isValidFloat(fieldname , fieldvalue) 16. Validation for field which allows only float numbers isFloat(fieldname , fieldvalue) 17. Validation for special characters like < and >. isValid(fieldname , fieldvalue) 18. Credit card validations for diffrent cards CheckCardNumber(frmObj) */ function checkTrim(txtString) { txtString = LTrim(txtString); txtString = RTrim(txtString); return txtString; } //returns the string after deleting the trailing spaces function LTrim(txtString) { ctr = 0; while( ctr < txtString.length && (txtString.substring(ctr,ctr+1) == " ")) { ctr=ctr+1; } return txtString.substring(ctr); } // returns the string after deleting the leading spaces function RTrim(txtString) { ctr = txtString.length; while( ctr > 0 && (txtString.substring(ctr,ctr-1) == " ")) { ctr = ctr - 1; } return txtString.substring(0,ctr); } //Validation for field which should not be empty function isEmpty(fieldname,fieldvalue) { var str=checkTrim(fieldvalue) if(str.length==0) { alert(fieldname + ' cannot be blank '); return true; } return false; } //Validation for field which should not be selected function isSelected(fieldname,fieldvalue) { var str=checkTrim(fieldvalue) if(str.length==0) { alert(fieldname + ' not selected '); return true; } return false; } //Validation for field which can contain only alphanumeric value function hasOnlyAlphaNumeric(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') ))) { alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z) and (0 to 9) '); return false; } i++; } return true; } function hasOnlyAlphaNumericWithDotUnderScore(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) <= '.')||(str.charAt(i) <= '_'))) { alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z),(0 to 9), Dot and Underscore'); return false; } i++; } return true; } function hasOnlyAlphaNumericwithDot(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) == '.')||(str.charAt(i) <= ' '))) { alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z) and (0 to 9) '); return false; } i++; } return true; } function hasOnlyAlphaNumericWithSymbol(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||(str.charAt(i) <= '@')||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) <= ' '))) { alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z) and (0 to 9) and @'); return false; } i++; } return true; } //Validation for field which cannot contain Space inbetween. function isSpace(fieldname , fieldvalue) { var str=fieldvalue if((str).indexOf(" ")!=-1) { alert('Space is not allowed in '+fieldname); return false; } return true; } //Validation for field which cannot start with number function isStartsWithNumber(fieldname , fieldvalue) { var numbers = "0123456789"; startsWithNumber=false; var str = checkTrim(fieldvalue) for(i=0;i= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z')))) { alert(fieldname+' can contain only alphabets\n\nValid Characters: (A to Z),(a to z) '); return false; } i++; } return true; } function hasOnlyAlphabetsWithSpace(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))|| (str.charAt(i) == " "))) { alert(fieldname+' can contain only alphabets\n\nValid Characters: (A to Z),(a to z) And Space '); return false; } i++; } return true; } function hasOnlyAlphabetsAndSpecificChar(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') || (str.charAt(i) == " ") || (str.charAt(i) == "-") || (str.charAt(i) == "_") || (str.charAt(i) == ",") || (str.charAt(i) == ".") || (str.charAt(i) == "!") || (str.charAt(i) == "$") ||(str.charAt(i) == "'") || (str.charAt(i) >= "0") && (str.charAt(i) <= "9")))) { alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z),(0 to 9),($,_,!,.,-)whitespace and hyphen '); return false; } i++; } return true; } function hasOnlyAlphabetsAndSpecificCharBrackets(fieldname , fieldvalue) { var fieldString = checkTrim(fieldvalue); var checkString = '~^><'; for(i=0;i= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') || (str.charAt(i) == " ") || (str.charAt(i) == "-") || (str.charAt(i) == "_") || (str.charAt(i) == ",") || (str.charAt(i) == ".") || (str.charAt(i) == "!") || (str.charAt(i) == "$") || (str.charAt(i) == "(") || (str.charAt(i) == ")") || (str.charAt(i) == "[") || (str.charAt(i) == "]") || (str.charAt(i) == "@") || (str.charAt(i) == "#") || (str.charAt(i) == "+") || (str.charAt(i) == ";") || (str.charAt(i) == "\\")||(str.charAt(i) == "'") || (str.charAt(i) >= "0") && (str.charAt(i) <= "9")))) { alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z),(0 to 9),($,_,!,.,-,@,#,+,[,],(,),;)whitespace and hyphen '); return false; } i++; } return true; } */ //Validation for field which allows only numbers function hasOnlyNumeric(fieldname , fieldvalue) { var str = fieldvalue; var i = 0; while(i < str.length) { if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))) { alert(fieldname+' can contain only numeric value'); return false; } else { i = i + 1; } } return true; } function hasOnlyNumericAndSpecificChar(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9") || (str.charAt(i) == "-") || (str.charAt(i) == " ") || (str.charAt(i) == ",") || (str.charAt(i) == "(") || (str.charAt(i) == ")") || (str.charAt(i) == "+") || ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z')) || ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z')) ) ) { alert(fieldname+' can contain only numeric value,whitespace and hyphen'); return false; } i++; } return true; } function hasOnlyNumericAndComma(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9") || (str.charAt(i) == ",") ) ) { alert(fieldname+' can contain only numeric value and comma'); return false; } i++; } return true; } function hasOnlyNumericAndDot(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9") || (str.charAt(i) == ".") ) ) { alert(fieldname+' can contain only numeric value and dot'); return false; } i++; } return true; } //Validation for length of the field function isTooLong(fieldName,checkStr,length) { checkStr = checkTrim(checkStr); if((checkStr.length)>length) { alert (fieldName+' cannot exceed ' + length + ' character'); return true; // true if the length exceeds } else return false; // else false } //Validation for length of the field function isTooShort(fieldName,checkStr,length) { checkStr = checkTrim(checkStr); if((checkStr.length) < length) { alert (fieldName+' cannot shorter than ' + length + ' character'); return true; // true if the length short } else return false; // else false } //Check for Valid Email. function validateEmail(fieldname,frmField) { //Validating the email field //var emailRegxp = /^([\w]+)(.[\w]+){1,4}@([\w]+)(.[\w]+)([.][\w]{2,3}){1,2}$/; var emailRegxp = /^([\w-']+(?:\.[\w-']+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/ ; //var emailRegxp = /^[A-Za-z][A-Za-z0-9_'-.]+((?:\.[\w-']+)*)@([\w-]+(?:\.[\w-]+)*)(\.[\w]{2,3}){1,2}$/; if (!frmField.match(emailRegxp)) { alert('Invalid '+fieldname+' '); return (false); } return(true); }/* function validateEmail(fieldname, fieldvalue) { var fieldString = checkTrim(fieldvalue); var checkString = " ~!$%^*()+=?><,;#&|\/:"; for(i=0;i= "0") && (str.charAt(i) <= "9"))|| (str.charAt(i) == '/'))) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } else { if(str.charAt(i) == '/') count=count+1; } i++; } if(count>2) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } month=dtDate.substring(0,2); dat=dtDate.substring(3,5); year=dtDate.substring(6,10); if(month>12) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } if(month==1 || month==3 ||month==5||month==7||month==8||month==10||month==12) { if(dat>31) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } } if(month==2 || month==4 ||month==6||month==9||month==11) { if(dat>30) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } } if((year%4==0 && year%100!=0) || year%400==0) { if(month==2) { if(dat>29) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } } } else { if(month==2) { if(dat>28) { alert("Please enter valid Date (e.g. 12/27/2002)"); return false; } } } if(year < 1900 || year > 2050) { alert("Please enter year between 1900 and 2050"); return false; } return true; } // Validate Date function validateDate(startDate,endDate) { var TodayDate var stDate TodayDate = new Date() stDate = new Date(startDate) enDate = new Date(endDate) if (TodayDate < stDate) { alert("From Date cannot be the future date") return false; } if (TodayDate < enDate) { alert("To Date cannot be the future date") return false; } if (enDate < stDate) { alert("Invalid Date range selection"); return false; } return true; } // Validate Date function isDateBefore(date1Name,date1Value,date2Name,date2Value) { //check that the renew date is not lesser than the date rented var vDate1 = convertStringToDate(date1Value,5); var vDate2= convertStringToDate(date2Value,5); if(vDate1vDate2) { alert(date1Name+" cannot be later than the "+date2Name+"."); return false; } return true; } //Validation for field which cannot contains symebol function isValidString(fieldname , fieldvalue) { var fieldString = checkTrim(fieldvalue); var checkString = '~!@$%^*()-+=?><"'; for(i=0;i<,:;'/\\"; for(i=0;i= "0") && (str.charAt(i) <= "9"))) && (str.charAt(i) != ".") && (str.charAt(i) != "-")) { alert(fieldname+' is not valid\n\n e.g -57.55'); return false; } if(str.charAt(i) == ".") j++; if(str.charAt(i) == "-"){ if( i != 0 ){ alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } } i++; } if(j>1) { alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } if(str.indexOf('.')>=0) { str1=str.substring(str.indexOf('.'),str.length-1); if(str1.length>2) { alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } } return true; } //Validation for field which allows only numbers function isFloat(fieldname , fieldvalue) { var str = fieldvalue; var str1; i = 0; j=0; if(str.charAt(0)==".") { alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } while(i < str.length) { if((!((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))) && (str.charAt(i) != ".")) { alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } if(str.charAt(i) == ".") j++; i++; } if(j>1) { alert(fieldname+' is not valid\n\n e.g 57.55'); return false; } if(str.indexOf('.')>=0) { str1=str.substring(str.indexOf('.'),str.length-1); if(str1.length>2) { alert(fieldname+' is not valid\n\nOnly 2 digits allowed after the decimal'); return false; } } return true; } //Validation for field which can not allow < and > and ". function isValid(fieldname , fieldvalue) { var str = fieldvalue; i = 0; while(i < str.length) { if((str.charAt(i) == '<') || (str.charAt(i) == '>') || (str.charAt(i) == '\"') || (str.charAt(i) == '\'') || (str.charAt(i) == ' ')) { alert('Invalid '+ fieldname+''); return false; } i++; } return true; } function chkNAN(char2chk) { var validNum = "0123456789"; if (validNum.indexOf(char2chk) == "-1") { alert("You have entered a non-numeric character."); return false; } } /*Credit card validations for diffrent cards */