Guest User

Untitled

a guest
Nov 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. var dictionary = [
  2. ["select","insert","update","delete","drop","--","alter","xp_","execute","declare","information_schema","table_cursor"], /* the bad words */
  3. ["select","insert","update","delete","drop","--","alter","xp\_","execute","declare","information_schema","table_cursor"], /* for regexp */
  4. ["se_lect","in_sert","up_date","de_lete","dr_op","-","al_ter","x.p.","exe_cute","de_clare","infoschema","tablecursor"] /* the replacements */
  5. ]
  6. var centerbox = document.getElementById("centerbox");
  7. var allinputs=centerbox.getElementsByTagName('input'),
  8. alltextarea=centerbox.getElementsByTagName('textarea');
  9.  
  10. jQuery(function($) {
  11.  
  12.  
  13. var symbolsLibrary = [
  14. ["(",")","&#59;", "#", """],
  15. ["(",")","&#59;", "#", '"'], // for reg - replace all instances
  16. ["(", ")", ";", "#", '"']
  17. ];
  18.  
  19. var badSymbol,i,m,temp,
  20. found = [];
  21.  
  22. loopThruFields(allinputs);
  23. loopThruFields(alltextarea);
  24.  
  25. //PHONE FORMAT
  26. $('.custom_form input[name*=phone]:text').addClass('phonemask');
  27. $.getScript("common/js/jquery.maskedinput.js",function(){
  28. $(".phonemask").mask("(999) 999-9999");
  29. });
  30.  
  31. //VALIDATE EMAIL ON BLUR
  32. /*$('.custom_form input[name*=email]:text').each(function(){
  33. $(this).blur(function() {
  34. if(isValidEmailAddress($(this).val())==false){
  35. inlineMsg(this.id, 'Please enter a valid email address', 0);
  36. setFocus(this);
  37. return false;
  38. }
  39. });
  40. });*/
  41. function setFocus(obj){
  42. function focusObj(){
  43. $(obj).focus();
  44. clearTimeout(t);
  45. }
  46. var t = setTimeout(focusObj, 300);
  47. }
  48.  
  49. function loopThruFields(allFields) {
  50. for (i = 0; i < allFields.length; i++) {
  51. temp = allFields[i].value;
  52. for (m=0; m<symbolsLibrary[0].length; m++) {
  53. badSymbol = symbolsLibrary[0][m];
  54. if (temp.indexOf(badSymbol) >-1) {
  55. found.push(badSymbol);
  56. allFields[i].value = allFields[i].value.replace(new RegExp(symbolsLibrary[0][m], 'g'), symbolsLibrary[2][m]); //Replace ASCII Codes
  57. }
  58. } // end m loop
  59. var currentFocus = allFields[i].getAttribute('onFocus');
  60. if (allFields[i].type == 'text' || allFields[i].type == 'textarea') {
  61. if (allFields[i].getAttribute('onFocus') ==null) {
  62. allFields[i].onfocus = function() {
  63. wordCheck(this);
  64. }
  65. }
  66. //ZIP NUMBERS ONLY
  67. if (allFields[i].name.toLowerCase().indexOf("zip") !==-1) {
  68. allFields[i].onfocus = function() {
  69. onlyNumbers(this);
  70. }
  71. }
  72. }
  73. } // end iloop
  74. } // end loopThruFields
  75.  
  76. var submitBtn = document.getElementById("submitButton");
  77. if (submitBtn) {
  78. changeInputType(submitBtn, "button")
  79. }
  80. });
  81.  
  82. function changeInputType(oldObject, oType) {
  83. var newObject = document.createElement('input');
  84. newObject.onclick = function() {
  85. validateForm();
  86. }
  87. newObject.type = oType;
  88. // 'Copy' old types to new element
  89. if(oldObject.size)
  90. newObject.size = oldObject.size;
  91. if(oldObject.value)
  92. newObject.value = oldObject.value;
  93. if(oldObject.name)
  94. newObject.name = oldObject.name;
  95. if(oldObject.id)
  96. newObject.id = oldObject.id;
  97. if(oldObject.className)
  98. newObject.className = oldObject.className;
  99. // replace old object with new one
  100. oldObject.parentNode.replaceChild(newObject,oldObject);
  101. return newObject;
  102. }
  103.  
  104. // ################ START VALIDATION ######################### \\
  105. // validates elements whose ID starts with "req_".
  106. function validateForm() {
  107.  
  108. var myArray = new Array(),
  109. myTags=document.getElementsByTagName('*');
  110. var isvalid = true;
  111.  
  112. for (var s=0; s<myTags.length; s++) {
  113. if (myTags[s].id.match('req_') != null) {
  114. var geta = document.getElementById(myTags[s].id);
  115. myArray[s] = geta;
  116. }
  117. }
  118. loopThruFields2(allinputs);
  119. loopThruFields2(alltextarea);
  120.  
  121. for (var i in myArray) {
  122. if (myArray[i].value == ""){
  123. inlineMsg(myArray[i].id,'<strong>Required</strong>',2);
  124. return false;
  125. }
  126. }
  127. $('.custom_form input[name*=email]:text').each(function(){ //VALIDATE EMAIL
  128. $(this).val($(this).val().replace(/\s/g, ""));
  129. //alert(emailTrim);
  130. if(isValidEmailAddress($(this).val())==false) { // if not true show validation
  131. inlineMsg(this.id, 'Please enter a valid email address', 2);
  132. isvalid = false;
  133. }
  134. else
  135. isvalid = true;
  136. });
  137.  
  138. if(isvalid)
  139. //document.custom_form.submit();
  140. alert('submit');
  141. }
  142.  
  143. function loopThruFields2(allFields) {
  144. var temp, badSymbol, found = [];
  145. for (var j = 0; j < allFields.length; j++) {
  146. temp = allFields[j].value;
  147. for (var m=0; m<dictionary[0].length; m++) {
  148. badSymbol = dictionary[0][m];
  149. if (temp.indexOf(badSymbol) >-1) {
  150. found.push(badSymbol);
  151. allFields[j].value = allFields[j].value.replace(new RegExp(dictionary[0][m], 'g'), dictionary[2][m]); //Replace ASCII Codes
  152. }
  153. }
  154. }
  155. }
  156.  
  157. // ################ START TEXT LENGTH LIMITS ######################### \\
  158. function limitText(formField, messageDiv, limitNum) {
  159. var countDownId = document.getElementById(messageDiv);
  160. countDownId.style.display = "inline";
  161. countDownId.innerHTML = limitNum - formField.value.length + " characters left.";
  162. formField.onblur=function() {
  163. countDownId.style.display = "none";
  164. };
  165. formField.onkeydown= function() {countdownEvent(); };
  166. formField.onkeyup= function() {countdownEvent(); };
  167. function countdownEvent() {
  168. if (formField.value.length > limitNum) {
  169. formField.value = formField.value.substring(0, limitNum);
  170. } else {
  171. countDownId.innerHTML = limitNum - formField.value.length + " characters left.";
  172. }
  173. replaceFromDictionary(formField);
  174. };
  175.  
  176. replaceFromDictionary(formField);
  177. }
  178.  
  179. // ################ SQL WORDS VALIDATION ######################### \\
  180. function wordCheck(formField) {
  181. formField.onkeydown= function() {listenEvent(); };
  182. formField.onkeyup= function() {listenEvent(); };
  183. function listenEvent() {
  184. replaceFromDictionary(formField);
  185. }
  186. replaceFromDictionary(formField);
  187. }
  188. function replaceFromDictionary(formfield) {
  189. var fixed = formfield.value,
  190. lcVal = formfield.value.toLowerCase(),
  191. i = dictionary[0].length,
  192. bad = '',
  193. reg = new RegExp();
  194. found = [];
  195.  
  196. for (i = 0; i < dictionary[0].length; i++) {
  197. var replaceBad, matchBad, badLowCase, pos, badCaseLower, newFixed = '';
  198. bad = dictionary[0][i];
  199. var temp = lcVal;
  200.  
  201. if (temp.indexOf(bad)>-1) { //lowercase word exists in library
  202. pos= temp.indexOf(bad);
  203. temp = fixed.substring(pos, (pos + bad.length)); //get bad value
  204. badCaseLower = temp.toLowerCase();
  205. newFixed = fixed.substr(0, pos) + badCaseLower; // = field value + lower case of bad value
  206. }
  207.  
  208. if (newFixed.indexOf(bad) > -1) {
  209. found.push(bad);
  210. fixed = newFixed.replace(new RegExp(dictionary[1][i], 'g'), dictionary[2][i]);
  211. }
  212. }
  213. if (found.length > 0) {
  214. formfield.value = fixed;
  215. failMsg(formfield, found);
  216. }
  217. }
  218.  
  219. function failMsg(formField, badwords) {
  220. if(formField.id == "") {
  221. var newDate = new Date();
  222. formField.id = "newId"+ newDate.getTime();
  223. }
  224. var multiple, bad = '';
  225. if (badwords.length == 1) {
  226. multiple = false;
  227. bad = badwords[0];
  228. } else {
  229. multiple = true;
  230. badwords[badwords.length-1] = 'and ' + badwords[badwords.length-1];
  231. bad = badwords.join(', ');
  232. }
  233. inlineMsg(formField.id, 'We\'re sorry, we have found the word' + (multiple ? 's' : '') +' <b>'+bad+'</b> in this field. ' + (multiple ? 'These words are' : 'This word is') + ' not allowed in our webform, per Congressional regulations. We have chosen alternate words to use in your submission; please review the changes in the form before you submit it.',7);
  234. }
  235.  
  236.  
  237. // ################ LIMIT CHARACTERS TO NUMBERS ONLY ########### //
  238. function onlyNumbers(objectName){
  239. validateNums(objectName);
  240. objectName.onkeydown= function() {validateNums(objectName);};
  241. objectName.onkeyup= function() {validateNums(objectName);};
  242. }
  243. function validateNums(objectName){
  244. var checkOK = "0123456789 -)(",
  245. checkStr = objectName,
  246. allValid = true,
  247. decPoints = 0,
  248. allNum = "",
  249. ch, i;
  250. for (i = 0; i < checkStr.value.length; i++) {
  251. ch = checkStr.value.charAt(i);
  252. for (var j = 0; j < checkOK.length; j++)
  253. if (ch == checkOK.charAt(j)) break;
  254. if (j == checkOK.length) {
  255. allValid = false;
  256. break;
  257. }
  258. if (ch != ",") allNum += ch;
  259. }
  260. if (!allValid) {
  261. var str = checkStr.value;
  262. objectName.value = str.replace(ch, "");
  263. inlineMsg(objectName.id, 'Character "<b>'+ch+'</b>" is not allowed. Only numbers are allowed for this field', 0)
  264. //return (false);
  265. } else {
  266. //hideMessage();
  267. }
  268. }
  269.  
  270. // ################ EMAIL ADDRESS FORMAT VALIDATION ##################//
  271.  
  272. function isValidEmailAddress(emailAddress) { // validate email and return true if valid
  273. var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  274. return pattern.test(emailAddress);
  275. }
  276.  
  277.  
  278. function hideMessage() {
  279. var errMsg = document.getElementById('errMsg');
  280. if(errMsg)
  281. errMsg.style.display = 'none';
  282. }
  283.  
  284. // ################ inlineMsg BOX ################ //
  285. var MSGTIMER = 20,
  286. MSGSPEED = 5,
  287. MSGOFFSET = 3,
  288. MSGHIDE = 3;
  289. // build out the divs, set attributes and call the fade function //
  290. function inlineMsg(target,string,autohide) {
  291. var msg;
  292. var msgcontent;
  293. if(!document.getElementById('errMsg')) {
  294. msg = document.createElement('div');
  295. msg.id = 'errMsg';
  296. msgcontent = document.createElement('div');
  297. msgcontent.id = 'msgcontent';
  298. document.body.appendChild(msg);
  299. msg.appendChild(msgcontent);
  300. msg.style.filter = 'alpha(opacity=0)';
  301. msg.style.opacity = 0;
  302. msg.alpha = 0;
  303. } else {
  304. msg = document.getElementById('errMsg');
  305. msgcontent = document.getElementById('msgcontent');
  306. }
  307. msgcontent.innerHTML = string;
  308. msg.style.position = 'absolute';
  309. msg.style.zIndex = 200;
  310. msg.style.display = 'block';
  311. msgcontent.style.display = 'block';
  312. var msgheight = msg.offsetHeight;
  313.  
  314. /*var targetdiv = document.getElementById(target);*/
  315.  
  316. var targetdiv = $('#'+target);
  317. $('#'+target).focus();
  318.  
  319. var targetheight = targetdiv.height();//targetdiv.offsetHeight;
  320. var targetwidth = targetdiv.width();//targetdiv.offsetWidth;
  321. var topposition = targetdiv.offset().top //topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  322. topposition = topposition - ((msgheight - targetheight) / 2);
  323. var leftposition = targetdiv.offset().left + targetwidth + MSGOFFSET ; //leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  324. msg.style.top = topposition + 'px';
  325. msg.style.left = leftposition + 'px';
  326. clearInterval(msg.timer);
  327. msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  328. if(!autohide) {
  329. autohide = MSGHIDE;
  330. }
  331. window.setTimeout("hideMsg()", (autohide * 1000));
  332. }
  333.  
  334. // hide the form alert //
  335. function hideMsg(msg) {
  336. var msg = document.getElementById('errMsg');
  337. if(!msg.timer) {
  338. msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  339. }
  340. }
  341.  
  342. // face the message box //
  343. function fadeMsg(flag) {
  344. if(flag == null) {
  345. flag = 1;
  346. }
  347. var msg = document.getElementById('errMsg');
  348. var value;
  349. if(flag == 1) {
  350. value = msg.alpha + MSGSPEED;
  351. } else {
  352. value = msg.alpha - MSGSPEED;
  353. }
  354. msg.alpha = value;
  355. msg.style.opacity = (value / 100);
  356. msg.style.filter = 'alpha(opacity=' + value + ')';
  357. if(value >= 99) {
  358. clearInterval(msg.timer);
  359. msg.timer = null;
  360. } else if(value <= 1) {
  361. msg.style.display = "none";
  362. clearInterval(msg.timer);
  363. }
  364. }
  365.  
  366. // calculate the position of the element in relation to the left of the browser //
  367. function leftPosition(target) {
  368. var left = 0;
  369. if(target.offsetParent) {
  370. while(1) {
  371. left += target.offsetLeft;
  372. if(!target.offsetParent) {
  373. break;
  374. }
  375. target = target.offsetParent;
  376. }
  377. } else if(target.x) {
  378. left += target.x;
  379. }
  380. return left;
  381. }
  382.  
  383. // calculate the position of the element in relation to the top of the browser window //
  384. function topPosition(target) {
  385. var top = 0;
  386. if(target.offsetParent) {
  387. while(1) {
  388. top += target.offsetTop;
  389. if(!target.offsetParent) {
  390. break;
  391. }
  392. target = target.offsetParent;
  393. }
  394. } else if(target.y) {
  395. top += target.y;
  396. }
  397. return top;
  398. }
  399.  
  400. // preload the arrow //
  401. if(document.images) {
  402. arrow = new Image(7,80);
  403. arrow.src = "common/images/msg_arrow.gif";
  404. }
Add Comment
Please, Sign In to add comment