PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/scripts/libs/rsv.js

https://gitlab.com/adamlwalker/generatedata
JavaScript | 243 lines | 231 code | 4 blank | 8 comment | 46 complexity | ebc78f651810e53215e1c084e7c73bf2 MD5 | raw file
  1. /**
  2. * rsv.js - Really Simple Validation
  3. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. *
  5. * v2.5.2, Jan 30 2010 - http://www.benjaminkeen.com/software/rsv
  6. */
  7. if(typeof rsv=='undefined') rsv={};
  8. // SETTINGS
  9. rsv.displayType="alert-all";
  10. rsv.errorFieldClass=null;
  11. rsv.errorTextIntro="Please fix the following error(s) and resubmit:";
  12. rsv.errorJSItemBullet="* ";
  13. rsv.errorHTMLItemBullet="• ";
  14. rsv.errorTargetElementId="rsvErrors";
  15. rsv.customErrorHandler=null;
  16. rsv.onCompleteHandler=null;
  17. // ~~~~~~~~~~~~~~~~~~~~~~~
  18. rsv.validate=function(form,rules)
  19. {rsv.returnHash=[];for(var i=0;i<rules.length;i++)
  20. {var row=rules[i].replace(/\\,/ig,"%%C%%");row=row.split(",");var satisfiesIfConditions=true;while(row[0].match("^if:"))
  21. {var cond=row[0];cond=cond.replace("if:","");var comparison="equal";var parts=[];if(cond.search("!=")!=-1)
  22. {parts=cond.split("!=");comparison="not_equal";}
  23. else
  24. parts=cond.split("=");var fieldToCheck=parts[0];var valueToCheck=parts[1];var fieldnameValue="";if(form[fieldToCheck].type==undefined)
  25. {for(var j=0;j<form[fieldToCheck].length;j++)
  26. {if(form[fieldToCheck][j].checked)
  27. fieldnameValue=form[fieldToCheck][j].value;}}
  28. else if(form[fieldToCheck].type=="checkbox")
  29. {if(form[fieldToCheck].checked)
  30. fieldnameValue=form[parts[0]].value;}
  31. else
  32. fieldnameValue=form[parts[0]].value;if(comparison=="equal"&&fieldnameValue!=valueToCheck)
  33. {satisfiesIfConditions=false;break;}
  34. else if(comparison=="not_equal"&&fieldnameValue==valueToCheck)
  35. {satisfiesIfConditions=false;break;}
  36. else
  37. row.shift();}
  38. if(!satisfiesIfConditions)
  39. continue;var requirement=row[0];var fieldName=row[1];var fieldName2,fieldName3,errorMessage,lengthRequirements,date_flag;if(requirement!="function"&&form[fieldName]==undefined)
  40. {alert("RSV Error: the field \""+fieldName+"\" doesn't exist! Please check your form and settings.");return false;}
  41. if(requirement!="function"&&rsv.errorFieldClass)
  42. {if(form[fieldName].type==undefined)
  43. {for(var j=0;j<form[fieldName].length;j++)
  44. rsv.removeClassName(form[fieldName][j],rsv.errorFieldClass,true);}
  45. else
  46. rsv.removeClassName(form[fieldName],rsv.errorFieldClass);}
  47. if(row.length==6)
  48. {fieldName2=row[2];fieldName3=row[3];date_flag=row[4];errorMessage=row[5];}
  49. else if(row.length==5)
  50. {fieldName2=row[2];fieldName3=row[3];errorMessage=row[4];}
  51. else if(row.length==4)
  52. {fieldName2=row[2];errorMessage=row[3];}
  53. else
  54. errorMessage=row[2];if(requirement.match("^length"))
  55. {lengthRequirements=requirement;requirement="length";}
  56. if(requirement.match("^range"))
  57. {rangeRequirements=requirement;requirement="range";}
  58. switch(requirement)
  59. {case"required":if(form[fieldName].type==undefined)
  60. {var oneIsChecked=false;for(var j=0;j<form[fieldName].length;j++)
  61. {if(form[fieldName][j].checked)
  62. oneIsChecked=true;}
  63. if(!oneIsChecked)
  64. {if(!rsv.processError(form[fieldName],errorMessage))
  65. return false;}}
  66. else if(form[fieldName].type=="select-multiple")
  67. {var oneIsSelected=false;for(var k=0;k<form[fieldName].length;k++)
  68. {if(form[fieldName][k].selected)
  69. oneIsSelected=true;}
  70. if(!oneIsSelected||form[fieldName].length==0)
  71. {if(!rsv.processError(form[fieldName],errorMessage))
  72. return false;}}
  73. else if(form[fieldName].type=="checkbox")
  74. {if(!form[fieldName].checked)
  75. {if(!rsv.processError(form[fieldName],errorMessage))
  76. return false;}}
  77. else if(!form[fieldName].value)
  78. {if(!rsv.processError(form[fieldName],errorMessage))
  79. return false;}
  80. break;case"digits_only":if(form[fieldName].value&&form[fieldName].value.match(/\D/))
  81. {if(!rsv.processError(form[fieldName],errorMessage))
  82. return false;}
  83. break;case"letters_only":if(form[fieldName].value&&form[fieldName].value.match(/[^a-zA-Z]/))
  84. {if(!rsv.processError(form[fieldName],errorMessage))
  85. return false;}
  86. break;case"is_alpha":if(form[fieldName].value&&form[fieldName].value.match(/\W/))
  87. {if(!rsv.processError(form[fieldName],errorMessage))
  88. return false;}
  89. break;case"custom_alpha":var conversion={"L":"[A-Z]","V":"[AEIOU]","l":"[a-z]","v":"[aeiou]","D":"[a-zA-Z]","F":"[aeiouAEIOU]","C":"[BCDFGHJKLMNPQRSTVWXYZ]","x":"[0-9]","c":"[bcdfghjklmnpqrstvwxyz]","X":"[1-9]","E":"[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]"};var reg_exp_str="";for(var j=0;j<fieldName2.length;j++)
  90. {if(conversion[fieldName2.charAt(j)])
  91. reg_exp_str+=conversion[fieldName2.charAt(j)];else
  92. reg_exp_str+=fieldName2.charAt(j);}
  93. var reg_exp=new RegExp(reg_exp_str);if(form[fieldName].value&&reg_exp.exec(form[fieldName].value)==null)
  94. {if(!rsv.processError(form[fieldName],errorMessage))
  95. return false;}
  96. break;case"reg_exp":var reg_exp_str=fieldName2.replace(/%%C%%/ig, ",");if(row.length==5)
  97. var reg_exp=new RegExp(reg_exp_str,fieldName3);else
  98. var reg_exp=new RegExp(reg_exp_str);if(form[fieldName].value&&reg_exp.exec(form[fieldName].value)==null)
  99. {if(!rsv.processError(form[fieldName],errorMessage))
  100. return false;}
  101. break;case"length":comparison_rule="";rule_string="";if(lengthRequirements.match(/length=/))
  102. {comparison_rule="equal";rule_string=lengthRequirements.replace("length=","");}
  103. else if(lengthRequirements.match(/length>=/))
  104. {comparison_rule="greater_than_or_equal";rule_string=lengthRequirements.replace("length>=","");}
  105. else if(lengthRequirements.match(/length>/))
  106. {comparison_rule="greater_than";rule_string=lengthRequirements.replace("length>","");}
  107. else if(lengthRequirements.match(/length<=/))
  108. {comparison_rule="less_than_or_equal";rule_string=lengthRequirements.replace("length<=","");}
  109. else if(lengthRequirements.match(/length</))
  110. {comparison_rule="less_than";rule_string=lengthRequirements.replace("length<","");}
  111. switch(comparison_rule)
  112. {case"greater_than_or_equal":if(!(form[fieldName].value.length>=parseInt(rule_string)))
  113. {if(!rsv.processError(form[fieldName],errorMessage))
  114. return false;}
  115. break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)))
  116. {if(!rsv.processError(form[fieldName],errorMessage))
  117. return false;}
  118. break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)))
  119. {if(!rsv.processError(form[fieldName],errorMessage))
  120. return false;}
  121. break;case"less_than":if(!(form[fieldName].value.length<parseInt(rule_string)))
  122. {if(!rsv.processError(form[fieldName],errorMessage))
  123. return false;}
  124. break;case"equal":var range_or_exact_number=rule_string.match(/[^_]+/);var fieldCount=range_or_exact_number[0].split("-");if(fieldCount.length==2)
  125. {if(form[fieldName].value.length<fieldCount[0]||form[fieldName].value.length>fieldCount[1])
  126. {if(!rsv.processError(form[fieldName],errorMessage))
  127. return false;}}
  128. else
  129. {if(form[fieldName].value.length!=fieldCount[0])
  130. {if(!rsv.processError(form[fieldName],errorMessage))
  131. return false;}}
  132. break;}
  133. break;case"valid_email":if(form[fieldName].value&&!rsv.isValidEmail(form[fieldName].value))
  134. {if(!rsv.processError(form[fieldName],errorMessage))
  135. return false;}
  136. break;case"valid_date":var isLaterDate=false;if(date_flag=="later_date")
  137. isLaterDate=true;else if(date_flag=="any_date")
  138. isLaterDate=false;if(!rsv.isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate))
  139. {if(!rsv.processError(form[fieldName],errorMessage))
  140. return false;}
  141. break;case"same_as":if(form[fieldName].value!=form[fieldName2].value)
  142. {if(!rsv.processError(form[fieldName],errorMessage))
  143. return false;}
  144. break;case"range":comparison_rule="";rule_string="";if(rangeRequirements.match(/range=/))
  145. {comparison_rule="equal";rule_string=rangeRequirements.replace("range=","");}
  146. else if(rangeRequirements.match(/range>=/))
  147. {comparison_rule="greater_than_or_equal";rule_string=rangeRequirements.replace("range>=","");}
  148. else if(rangeRequirements.match(/range>/))
  149. {comparison_rule="greater_than";rule_string=rangeRequirements.replace("range>","");}
  150. else if(rangeRequirements.match(/range<=/))
  151. {comparison_rule="less_than_or_equal";rule_string=rangeRequirements.replace("range<=","");}
  152. else if(rangeRequirements.match(/range</))
  153. {comparison_rule="less_than";rule_string=rangeRequirements.replace("range<","");}
  154. switch(comparison_rule)
  155. {case"greater_than_or_equal":if(!(form[fieldName].value>=Number(rule_string)))
  156. {if(!rsv.processError(form[fieldName],errorMessage))
  157. return false;}
  158. break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)))
  159. {if(!rsv.processError(form[fieldName],errorMessage))
  160. return false;}
  161. break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)))
  162. {if(!rsv.processError(form[fieldName],errorMessage))
  163. return false;}
  164. break;case"less_than":if(!(form[fieldName].value<Number(rule_string)))
  165. {if(!rsv.processError(form[fieldName],errorMessage))
  166. return false;}
  167. break;case"equal":var rangeValues=rule_string.split("-");if((form[fieldName].value<Number(rangeValues[0]))||(form[fieldName].value>Number(rangeValues[1])))
  168. {if(!rsv.processError(form[fieldName],errorMessage))
  169. return false;}
  170. break;}
  171. break;case"function":custom_function=fieldName;eval("var result = "+custom_function+"()");if(result.constructor.toString().indexOf("Array")!=-1)
  172. {for(var j=0;j<result.length;j++)
  173. {if(!rsv.processError(result[j][0],result[j][1]))
  174. return false;}}
  175. break;default:alert("Unknown requirement flag in validateFields(): "+requirement);return false;}}
  176. if(typeof rsv.customErrorHandler=='function')
  177. return rsv.customErrorHandler(form,rsv.returnHash);else if(rsv.displayType=="alert-all")
  178. {var errorStr=rsv.errorTextIntro+"\n\n";for(var i=0;i<rsv.returnHash.length;i++)
  179. {errorStr+=rsv.errorJSItemBullet+rsv.returnHash[i][1]+"\n";rsv.styleField(rsv.returnHash[i][0],i==0);}
  180. if(rsv.returnHash.length>0)
  181. {alert(errorStr);return false;}}
  182. else if(rsv.displayType=="display-html")
  183. {var success=rsv.displayHTMLErrors(form,rsv.returnHash);if(!success)
  184. return false;}
  185. if(typeof rsv.onCompleteHandler=='function')
  186. return rsv.onCompleteHandler();else
  187. return true;}
  188. rsv.processError=function(obj,message)
  189. {message=message.replace(/%%C%%/ig,",");var continueProcessing=true;switch(rsv.displayType)
  190. {case"alert-one":alert(message);rsv.styleField(obj,true);continueProcessing=false;break;case"alert-all":case"display-html":rsv.returnHash.push([obj,message]);break;}
  191. return continueProcessing;}
  192. rsv.displayHTMLErrors=function(f,errorInfo)
  193. {var errorHTML=rsv.errorTextIntro+"<br /><br />";for(var i=0;i<errorInfo.length;i++)
  194. {errorHTML+=rsv.errorHTMLItemBullet+errorInfo[i][1]+"<br />";rsv.styleField(errorInfo[i][0],i==0);}
  195. if(errorInfo.length>0)
  196. {document.getElementById(rsv.errorTargetElementId).style.display="block";document.getElementById(rsv.errorTargetElementId).innerHTML=errorHTML;return false;}
  197. return true;}
  198. rsv.styleField=function(field,focus)
  199. {if(!rsv.errorFieldClass){return;}
  200. if(field.type==undefined)
  201. {if(focus)
  202. field[0].focus();for(var i=0;i<field.length;i++)
  203. rsv.addClassName(field[i],rsv.errorFieldClass,true);}
  204. else
  205. {if(rsv.errorFieldClass)
  206. rsv.addClassName(field,rsv.errorFieldClass,true);if(focus)
  207. field.focus();}}
  208. rsv.isValidEmail=function(str)
  209. {var str2=str.replace(/^\s*/,"");var s=str2.replace(/\s*$/,"");var at="@";var dot=".";var lat=s.indexOf(at);var lstr=s.length;var ldot=s.indexOf(dot);if(s.indexOf(at)==-1||(s.indexOf(at)==-1||s.indexOf(at)==0||s.indexOf(at)==lstr)||(s.indexOf(dot)==-1||s.indexOf(dot)==0||s.indexOf(dot)==lstr)||(s.indexOf(at,(lat+1))!=-1)||(s.substring(lat-1,lat)==dot||s.substring(lat+1,lat+2)==dot)||(s.indexOf(dot,(lat+2))==-1)||(s.indexOf(" ")!=-1))
  210. {return false;}
  211. return true;}
  212. rsv.isWhitespace=function(s)
  213. {var whitespaceChars=" \t\n\r\f";if((s==null)||(s.length==0))
  214. return true;for(var i=0;i<s.length;i++)
  215. {var c=s.charAt(i);if(whitespaceChars.indexOf(c)==-1)
  216. return false;}
  217. return true;}
  218. rsv.isValidDate=function(month,day,year,isLaterDate)
  219. {var daysInMonth;if((year%4==0)&&((year%100!=0)||(year%400==0)))
  220. daysInMonth=[31,29,31,30,31,30,31,31,30,31,30,31];else
  221. daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];if(!month||!day||!year)return false;if(1>month||month>12)return false;if(year<0)return false;if(1>day||day>daysInMonth[month-1])return false;if(isLaterDate)
  222. {var today=new Date();var currMonth=today.getMonth()+1;var currDay=today.getDate();var currYear=today.getFullYear();if(String(currMonth).length==1)currMonth="0"+currMonth;if(String(currDay).length==1)currDay="0"+currDay;var currDate=String(currYear)+String(currMonth)+String(currDay);if(String(month).length==1)month="0"+month;if(String(day).length==1)day="0"+day;incomingDate=String(year)+String(month)+String(day);if(Number(currDate)>Number(incomingDate))
  223. return false;}
  224. return true;}
  225. rsv.addClassName=function(objElement,strClass,mayAlreadyExist)
  226. {if(objElement.className)
  227. {var arrList=objElement.className.split(' ');if(mayAlreadyExist)
  228. {var strClassUpper=strClass.toUpperCase();for(var i=0;i<arrList.length;i++)
  229. {if(arrList[i].toUpperCase()==strClassUpper)
  230. {arrList.splice(i,1);i--;}}}
  231. arrList[arrList.length]=strClass;objElement.className=arrList.join(' ');}
  232. else
  233. objElement.className=strClass;}
  234. rsv.removeClassName=function(objElement,strClass)
  235. {if(objElement.className)
  236. {var arrList=objElement.className.split(' ');var strClassUpper=strClass.toUpperCase();for(var i=0;i<arrList.length;i++)
  237. {if(arrList[i].toUpperCase()==strClassUpper)
  238. {arrList.splice(i,1);i--;}}
  239. objElement.className=arrList.join(' ');}}