1// LiveValidation 1.4 (standalone version) 2// Copyright (c) 2007-2010 Alec Hill (www.livevalidation.com) 3// LiveValidation is licensed under the terms of the MIT License 4 5/*********************************************** LiveValidation class ***********************************/ 6 7 8var jqobjPrime = null; 9var show_error_combined = true; 10var lv_offset = function(currentOffset) { 11 return currentOffset; 12}; 13var LiveValidation = function(element, optionsObj) { 14 15 16 this.initialize(element, optionsObj); 17} 18 19function checkDay(day, month, year) { 20 var valid = false; 21 if (day >= 1) { 22 // For months with 30 days. 23 if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30) { 24 valid = true; 25 } 26 // For months with 31 days. 27 if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day <= 31) { 28 valid = true; 29 } 30 // For February. 31 if (month == 2) 32 { 33 if (day <= 28) { 34 valid = true; 35 } else if (day == 29) { 36 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { 37 valid = true; 38 } //else invalid 39 } 40 } 41 } //else date is not valid 42 return valid; 43} 44 45 46LiveValidation.VERSION = '1.4 standalone'; 47 48/** element types constants ****/ 49 50LiveValidation.TEXTAREA = 1; 51LiveValidation.TEXT = 2; 52LiveValidation.PASSWORD = 3; 53LiveValidation.CHECKBOX = 4; 54LiveValidation.SELECT = 5; 55LiveValidation.FILE = 6; 56LiveValidation.CHECKBOX_GF = 9; 57LiveValidation.RADIO = 7; 58 59/****** Static methods *******/ 60 61/** 62 * pass an array of LiveValidation objects and it will validate all of them 63 * 64 * @param validations {Array} - an array of LiveValidation objects 65 * @return {Bool} - true if all passed validation, false if any fail 66 */ 67LiveValidation.massValidate = function(validations) { 68 69 70 var returnValue = true; 71 72 73 for (var i = 0, len = validations.length; i < len; ++i) { 74 var valid = validations[i].validate(); 75 if (returnValue) 76 returnValue = valid; 77 } 78 return returnValue; 79} 80 81/****** prototype ******/ 82function hasClass(element, cls) { 83 return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1; 84} 85LiveValidation.prototype = { 86 validClass: 'LV_valid', 87 invalidClass: 'LV_invalid', 88 messageClass: 'LV_validation_message', 89 validFieldClass: 'LV_valid_field', 90 invalidFieldClass: 'LV_invalid_field', 91 /** 92 * initialises all of the properties and events 93 * 94 * @param - Same as constructor above 95 */ 96 initialize: function(element, optionsObj) { 97 var self = this; 98 99 if (!element) 100 throw new Error("LiveValidation::initialize - No element reference or element id has been provided!"); 101 this.element = element.nodeName ? element : document.getElementById(element); 102 103 if (!this.element) 104 throw new Error("LiveValidation::initialize - No element with reference or id of '" + element + "' exists!"); 105 // default properties that could not be initialised above 106 this.validations = []; 107 this.elementType = this.getElementType(); 108 this.form = this.element.form; 109 110 111 112 // options 113 var options = optionsObj || {}; 114 115 if (options.jqObj && !jqobjPrime) { 116 jqobjPrime = options.jqObj; 117 } 118 119 this.fieldType = options.fieldType; 120 if (typeof this.form == "undefined") { 121 var Forms = jqobjPrime(this.element).parents("form"); 122 this.form = Forms[0]; 123 } 124 125 this.element.onfocus = function(e) { 126 127 var elems = document.getElementsByClassName("LV_validation_message"); 128 129 if (elems.length > 0) { 130 for (var i = 0; i < elems.length; i++) { 131 var classes = elems[i].className; 132 elems[i].className = classes + " hide_validation"; 133 134 } 135 } 136 137 138 if (!jqobjPrime(this).hasClass("hasDatepicker")) { 139 e.target.className.replace(/\bhide_validation\b/, ''); 140 } 141 142 143 144 } 145 146 147 148 this.validMessage = options.validMessage || " "; 149 var node = options.insertAfterWhatNode || this.element; 150 this.insertAfterWhatNode = node.nodeType ? node : document.getElementById(node); 151 this.onlyOnBlur = options.onlyOnBlur || false; 152 this.wait = options.wait || 0; 153 this.onlyOnSubmit = options.onlyOnSubmit || false; 154 // hooks 155 this.beforeValidation = options.beforeValidation || function() { 156 }; 157 this.beforeValid = options.beforeValid || function() { 158 }; 159 this.onValid = options.onValid || function() { 160 this.insertMessage(this.createMessageSpan()); 161 this.addFieldClass(); 162 }; 163 this.afterValid = options.afterValid || function() { 164 }; 165 this.beforeInvalid = options.beforeInvalid || function() { 166 }; 167 this.onInvalid = options.onInvalid || function() { 168 this.insertMessage(this.createMessageSpan()); 169 this.addFieldClass(); 170 }; 171 this.afterInvalid = options.afterInvalid || function() { 172 }; 173 this.afterValidation = options.afterValidation || function() { 174 }; 175 176 // add to form if it has been provided 177 if (this.form) { 178 this.formObj = LiveValidationForm.getInstance(this.form); 179 180 this.formObj.addField(this); 181 } 182 // events 183 // collect old events 184 this.oldOnFocus = this.element.onfocus || function() { 185 }; 186 this.oldOnBlur = this.element.onblur || function() { 187 }; 188 189 if (this.fieldType == "checkbox") { 190 this.oldOnClick_check = {}; 191 192 193 var radioUL = this.element; 194
195 var len = radioUL.childNodes.length; 196 for (var i = 0; i < radioUL.childNodes.length; i++) { 197 this.oldOnClick_check[radioUL.childNodes[i].querySelector('input').id] = (radioUL.childNodes[i].querySelector('input').onclick || function() { 198 }); 199 } 200 201 202 } else { 203 this.oldOnClick_check = []; 204 this.oldOnClick = this.element.onclick || function() { 205 }; 206 } 207 208 this.oldOnChange = this.element.onchange || function() { 209 }; 210 this.oldOnKeyup = this.element.onkeyup || function() { 211 }; 212 this.element.onfocus = function(e) { 213 214 215 216 self.doOnFocus(e); 217 jqobjPrime(document).trigger("lv_field_on_focus", e); 218 return self.oldOnFocus.call(this, e); 219 } 220 if (!this.onlyOnSubmit) { 221 222 switch (this.elementType) { 223 224 225 226 227 case LiveValidation.CHECKBOX_GF: 228 229 var radioUL = this.element; 230 231 var len = radioUL.childNodes.length; 232 for (var i = 0; i < radioUL.childNodes.length; i++) { 233 234 235 236 237 radioUL.childNodes[i].querySelector('input').onclick = function(e, i) { 238 239 self.validate(); 240 241 return self.oldOnClick_check[this.id].call(this, e); 242 } 243 244 radioUL.childNodes[i].querySelector('input').onblur = function(e) { 245 246 return self.doOnBlur(e); 247 } 248 249 250 radioUL.childNodes[i].querySelector('input').onfocus = function(e) { 251 252 253 self.doOnFocus(e); 254 jqobjPrime(document).trigger("lv_field_on_focus", e); 255 return self.oldOnFocus.call(this, e); 256 } 257 258 259 260 261 } 262 break; 263 264 265 case LiveValidation.CHECKBOX: 266 267 case LiveValidation.RADIO: 268 269 case LiveValidation.SELECT: 270 271 272 this.element.onblur = function(e) { 273 274 275 if (hasSubFields.indexOf(self.fieldType) == -1) { 276 277 return self.doOnBlur(e); 278 } 279 else { 280 var elemLI = jqobjPrime(self.element).parents("li.gfield"); 281 282// 283 elemLI.find("input , select").each(function(k, v) { 284 285 286 var str = jQuery(this).attr("id"); 287 var getConfig = str.split("_"); 288 var separator = 'input_' + getConfig[1] + "_"; 289 var get_match = str.split(separator).pop(); 290 291 if (typeof all_validations[getConfig[1]][getConfig[2] + "_" + getConfig[3]] !== "undefined") { 292 293 294 all_validations[getConfig[1]][getConfig[2] + "_" + getConfig[3]].validate(); 295 } 296 }); 297 298 // return self.doOnBlur(e); 299 } 300 301 302 // self.oldOnBlur.call(this, e); 303 304 305 } 306 break; 307 case LiveValidation.FILE: 308 this.element.onchange = function(e) { 309 self.validate(); 310 return self.oldOnChange.call(this, e); 311 } 312 break; 313 default: 314 315 this.element.onblur = function(e) { 316 if (hasSubFields.indexOf(self.fieldType) == -1) { 317 318 return self.doOnBlur(e); 319 } 320 else { 321 var elemLI = jqobjPrime(self.element).parents("li.gfield"); 322 323// 324 elemLI.find("input , select").each(function(k, v) { 325 326 var str = jQuery(this).attr("id"); 327 var getConfig = str.split("_"); 328 var separator = 'input_' + getConfig[1] + "_"; 329 var get_match = str.split(separator).pop(); 330 331 if (typeof all_validations[getConfig[1]] !== "undefined" && typeof all_validations[getConfig[1]][getConfig[2] + "_" + getConfig[3]] !== "undefined") { 332 333 334 all_validations[getConfig[1]][getConfig[2] + "_" + getConfig[3]].validate(); 335 } 336 }); 337 338 // return self.doOnBlur(e); 339 } 340 341 342 // self.oldOnBlur.call(this, e); 343 } 344 } 345 } 346 }, 347 /** 348 * destroys the instance's events (restoring previous ones) and removes it from any LiveValidationForms 349 */ 350 destroy: function() { 351 if (this.formObj) { 352 // remove the field from the LiveValidationForm 353 this.formObj.removeField(this); 354 // destroy the LiveValidationForm if no LiveValidation fields left in it 355 this.formObj.destroy(); 356 } 357 // remove events - set them back to the previous events 358 this.element.onfocus = this.oldOnFocus; 359 if (!this.onlyOnSubmit) { 360 switch (this.elementType) { 361 case LiveValidation.CHECKBOX: 362 this.element.onclick = this.oldOnClick; 363 // let it run into the next to add a change event too 364 case LiveValidation.SELECT: 365 case LiveValidation.FILE:
366 this.element.onchange = this.oldOnChange; 367 break; 368 default: 369 if (!this.onlyOnBlur) 370 this.element.onkeyup = this.oldOnKeyup; 371 this.element.onblur = this.oldOnBlur; 372 } 373 } 374 this.validations = []; 375 this.removeMessageAndFieldClass(); 376 }, 377 /** 378 * adds a validation to perform to a LiveValidation object 379 * 380 * @param validationFunction {Function} - validation function to be used (ie Validate.Presence ) 381 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 382 * @return {Object} - the LiveValidation object itself so that calls can be chained 383 */ 384 add: function(validationFunction, validationParamsObj) { 385 this.validations.push({ 386 type: validationFunction, 387 params: validationParamsObj || {} 388 }); 389 return this; 390 }, 391 /** 392 * removes a validation from a LiveValidation object - must have exactly the same arguments as used to add it 393 * 394 * @param validationFunction {Function} - validation function to be used (ie Validate.Presence ) 395 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 396 * @return {Object} - the LiveValidation object itself so that calls can be chained 397 */ 398 remove: function(validationFunction, validationParamsObj) { 399 var victimless = []; 400 for (var i = 0, len = this.validations.length; i < len; i++) { 401 var v = this.validations[i]; 402 if (v.type != validationFunction && v.params != validationParamsObj) 403 victimless.push(v); 404 } 405 this.validations = victimless; 406 return this; 407 }, 408 /** 409 * makes the validation wait the alotted time from the last keystroke 410 */ 411 deferValidation: function(e) { 412 if (this.wait >= 300) 413 this.removeMessageAndFieldClass(); 414 var self = this; 415 if (this.timeout) 416 clearTimeout(self.timeout); 417 this.timeout = setTimeout(function() { 418 self.validate() 419 }, self.wait); 420 }, 421 /** 422 * sets the focused flag to false when field loses focus 423 */ 424 doOnBlur: function(e) { 425 426 this.focused = false; 427 this.validate(e); 428 }, 429 /** 430 * sets the focused flag to true when field gains focus 431 */ 432 doOnFocus: function(e) { 433 this.focused = true; 434 this.removeMessageAndFieldClass(); 435 }, 436 /** 437 * gets the type of element, to check whether it is compatible 438 * 439 * @param validationFunction {Function} - validation function to be used (ie Validate.Presence ) 440 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 441 */ 442 getElementType: function() { 443 444 445 446 var nn = this.element.nodeName.toUpperCase(); 447 var nt = this.element.type.toUpperCase(); 448 449 switch (true) { 450 case (nn == 'TEXTAREA'): 451 return LiveValidation.TEXTAREA; 452 case (nn == 'INPUT' && nt == 'TEXT'): 453 return LiveValidation.TEXT; 454 case (nn == 'INPUT' && nt == 'EMAIL'): 455 return LiveValidation.TEXT; 456 case (nn == 'INPUT' && nt == 'URL'): 457 return LiveValidation.TEXT; 458 case (nn == 'INPUT' && nt == 'TEL'): 459 return LiveValidation.TEXT; 460 case (nn == 'INPUT' && nt == 'NUMBER'): 461 return LiveValidation.TEXT; 462 case (nn == 'INPUT' && nt == 'PASSWORD'): 463 return LiveValidation.PASSWORD; 464 case (nn == 'INPUT' && nt == 'CHECKBOX'): 465 return LiveValidation.CHECKBOX; 466 case (nn == 'INPUT' && nt == 'RADIO'): 467 return LiveValidation.RADIO; 468 case (nn == 'INPUT' && nt == 'FILE'): 469 return LiveValidation.FILE; 470 case (nn == 'SELECT'): 471 return LiveValidation.SELECT; 472 case (nn == 'UL'): 473 return LiveValidation.CHECKBOX_GF; 474 case (nn == 'INPUT'): 475 throw new Error('LiveValidation::getElementType - Cannot use LiveValidation on an ' + nt.toLowerCase() + ' input!'); 476 default: 477 throw new Error('LiveValidation::getElementType - Element must be an input, select, or textarea - ' + nn.toLowerCase() + ' was given!'); 478 } 479 }, 480 /** 481 * loops through all the validations added to the LiveValidation object and checks them one by one 482 * 483 * @param validationFunction {Function} - validation function to be used (ie Validate.Presence ) 484 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 485 * @return {Boolean} - whether the all the validations passed or if one failed 486 */ 487 doValidations: function() { 488 this.validationFailed = false; 489 490 for (var i = 0, len = this.validations.length; i < len; ++i) { 491 492 this.validationFailed = !this.validateElement(this.validations[i].type, this.validations[i].params); 493 if (this.validationFailed) 494 return false; 495 } 496 this.message = this.validMessage; 497 return true; 498 }, 499 /** 500 * performs validation on the element and handles any error (validation or otherwise) it throws up 501 * 502 * @param validationFunction {Function} - validation function to be used (ie Validate.Presence ) 503 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 504 * @return {Boolean} - whether the validation has passed or failed 505 */
506 validateElement: function(validationFunction, validationParamsObj) { 507 // check whether we should display the message when empty 508 switch (validationFunction) { 509 case Validate.Presence: 510 case Validate.Confirmation: 511 case Validate.Acceptance: 512 this.displayMessageWhenEmpty = true; 513 break; 514 case Validate.Custom: 515 if (validationParamsObj.displayMessageWhenEmpty) 516 this.displayMessageWhenEmpty = true; 517 break; 518 } 519 if (this.elementType == LiveValidation.RADIO) { 520 value = this.element.checked; 521 } 522 else { 523 524 525 if (this.element.selectedIndex == -1 && this.elementType == LiveValidation.SELECT) { 526 var value = ""; 527 } else { 528 var value = (this.elementType == LiveValidation.SELECT) ? this.element.options[this.element.selectedIndex].value : this.element.value; 529 530 } 531 532 if (validationFunction == Validate.Acceptance || validationFunction == Validate.AcceptanceCheckbox) { 533 if (this.elementType != LiveValidation.CHECKBOX) 534 throw new Error('LiveValidation::validateElement - Element to validate acceptance must be a checkbox!'); 535 value = this.element.checked; 536 } 537 } 538 // now validate 539 var isValid = true; 540 try { 541 validationFunction(value, validationParamsObj); 542 } catch (error) { 543 if (error instanceof Validate.Error) { 544 if (value !== '' || (value === '' && this.displayMessageWhenEmpty)) { 545 this.validationFailed = true; 546 // Opera 10 adds stacktrace after newline 547 this.message = error.message.split('\n')[0]; 548 isValid = false; 549 } 550 } else { 551 throw error; 552 } 553 } finally { 554 return isValid; 555 } 556 }, 557 /** 558 * makes it do the all the validations and fires off the various callbacks 559 * 560 * @return {Boolean} - whether the all the validations passed or if one failed 561 */ 562 validate: function() { 563 564 if (!this.checkIfDisable(this.element)) { 565 this.beforeValidation(); 566 var isValid = this.doValidations(); 567 if (isValid) { 568 this.beforeValid(); 569 this.onValid(); 570 571 this.afterValid(this.element); 572 return true; 573 } else { 574 this.beforeInvalid(); 575 this.onInvalid(); 576 577 this.afterInvalid(this.element); 578 return false; 579 } 580 this.afterValidation(); 581 } else { 582 return true; 583 } 584 }, 585 /** 586 * enables the field 587 * 588 * @return {LiveValidation} - the LiveValidation object for chaining 589 */ 590 enable: function() { 591 592 this.element.removeAttribute("data-disabled"); 593 return this; 594 }, 595 /** 596 * disables the field and removes any message and styles associated with the field 597 * 598 * @return {LiveValidation} - the LiveValidation object for chaining 599 */ 600 disable: function() { 601 602 this.element.setAttribute("data-disabled", "yes"); 603 this.removeMessageAndFieldClass(); 604 return this; 605 }, 606 checkIfDisable: function(element) { 607 if (element.hasAttribute("data-disabled")) { 608 return true; 609 } 610 return false; 611 }, 612 /** Message insertion methods **************************** 613 * 614 * These are only used in the onValid and onInvalid callback functions and so if you override the default callbacks, 615 * you must either impliment your own functions to do whatever you want, or call some of these from them if you 616 * want to keep some of the functionality 617 */ 618 619 /** 620 * makes a span containg the passed or failed message 621 * 622 * @return {HTMLSpanObject} - a span element with the message in it 623 */ 624 createMessageSpan: function() { 625 var span = document.createElement('span'); 626 var textNode = document.createTextNode(this.message); 627 span.appendChild(textNode); 628 return span; 629 }, 630 /** 631 * inserts the element containing the message in place of the element that already exists (if it does) 632 * 633 * @param elementToIsert {HTMLElementObject} - an element node to insert 634 */ 635 insertMessage: function(elementToInsert) { 636 this.removeMessage(); 637 if (!this.validationFailed && !this.validMessage) 638 return; // dont insert anything if vaalidMesssage has been set to false or empty string 639 if ((this.displayMessageWhenEmpty 640 && (this.elementType == LiveValidation.CHECKBOX || this.element.value == '')) 641 || this.element.value != '') { 642 var className = this.validationFailed ? this.invalidClass : this.validClass; 643 elementToInsert.className += ' ' + this.messageClass + ' ' + className; 644 var parent = this.insertAfterWhatNode.parentNode; 645 if (this.insertAfterWhatNode.nextSibling) { 646 parent.insertBefore(elementToInsert, this.insertAfterWhatNode.nextSibling); 647 } else { 648 parent.appendChild(elementToInsert); 649 } 650 } 651 }, 652 /** 653 * changes the class of the field based on whether it is valid or not 654 */ 655 addFieldClass: function() { 656 this.removeFieldClass(); 657 if (!this.validationFailed) { 658 if (this.displayMessageWhenEmpty || this.element.value != '') { 659 if (this.element.className.indexOf(this.validFieldClass) == -1) 660 this.element.className += ' ' + this.validFieldClass; 661 } 662 } else { 663 if (this.element.className.indexOf(this.invalidFieldClass) == -1) 664 this.element.className += ' ' + this.invalidFieldClass; 665 } 666 667 668 }, 669 /** 670 * removes the message element if it exists, so that the new message will replace it 671 */ 672 removeMessage: function() { 673 var nextEl; 674 var el = this.insertAfterWhatNode; 675 while (el.nextSibling) { 676 if (el.nextSibling.nodeType === 1) { 677 nextEl = el.nextSibling; 678 break; 679 } 680 el = el.nextSibling; 681 } 682 if (nextEl && nextEl.className.indexOf(this.messageClass) != -1) 683 this.insertAfterWhatNode.parentNode.removeChild(nextEl); 684 }, 685 /** 686 * removes the class that has been applied to the field to indicte if valid or not 687 */ 688 removeFieldClass: function() { 689 var cn = this.element.className; 690 if (cn.indexOf(this.invalidFieldClass) != -1) 691 this.element.className = cn.split(this.invalidFieldClass).join(''); 692 if (cn.indexOf(this.validFieldClass) != -1) 693 this.element.className = cn.split(this.validFieldClass).join(' '); 694 }, 695 /** 696 * removes the message and the field class 697 */ 698 removeMessageAndFieldClass: function() { 699 this.removeMessage(); 700 this.removeFieldClass(); 701 } 702 703} 704 705/*************************************** LiveValidationForm class ****************************************/ 706/** 707 * This class is used internally by LiveValidation class to associate a LiveValidation field with a form it is icontained in one 708 * 709 * It will therefore not really ever be needed to be used directly by the developer, unless they want to associate a LiveValidation 710 * field with a form that it is not a child of, or add some extra functionality via the hooks (access through a LiveValidation object's formObj property) 711 */ 712 713/** 714 * handles validation of LiveValidation fields belonging to this form on its submittal 715 * 716 * @param element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm 717 */ 718var LiveValidationForm = function(element) { 719 this.initialize(element); 720} 721 722/** 723 * namespace to hold instances 724 */ 725LiveValidationForm.instances = {}; 726 727/** 728 * gets the instance of the LiveValidationForm if it has already been made or creates it if it doesnt exist 729 * 730 * @param element {mixed} - a dom element reference to or id of a form 731 */ 732LiveValidationForm.getInstance = function(element) { 733 if (!element) 734 throw new Error("LiveValidationForm::getInstance - No element reference or element id has been provided!"); 735 var el = element.nodeName ? element : document.getElementById(element); 736 var rand = Math.random() * Math.random(); 737 if (!el.id) 738 el.id = 'formId_' + rand.toString().replace(/\./, '') + new Date().valueOf();
739 if (!LiveValidationForm.instances[el.id]) 740 LiveValidationForm.instances[el.id] = new LiveValidationForm(el); 741 742 743 return LiveValidationForm.instances[el.id]; 744} 745 746 747 748 749LiveValidationForm.prototype = { 750 beforeValidation: function() { 751 }, 752 onValid: function() { 753 }, 754 onInvalid: function() { 755 }, 756 afterValidation: function() { 757 }, 758 /** 759 * constructor for LiveValidationForm - handles validation of LiveValidation fields belonging to this form on its submittal 760 * 761 * @param element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm 762 */ 763 initialize: function(element) { 764 765 766 this.name = element.id; 767 this.element = element; 768 this.fields = []; 769 // preserve the old onsubmit event 770 this.oldOnSubmit = this.element.onsubmit || function() { 771 }; 772 var self = this; 773 774 this.element.setAttribute("onsubmit", "return false;"); 775 var submitbutton = jQuery(this.element).find(':submit').attr('id'); 776 777 778 this.element.onsubmit = function(e) { 779 780 781 782 var moveTo = 0; 783 var elem_form = e.currentTarget; 784 785 if ("undefined" === typeof elem_form) { 786 return true; 787 } 788 789 790 791 if (hasClass(elem_form, "back_bt_press")) { 792 return true; 793 } 794 795 if (hasClass(elem_form, "save_bt_press")) { 796 return true; 797 } 798 var ret = false; 799 800 801 self.beforeValidation(), 802 self.valid = LiveValidation.massValidate(self.fields); 803 self.valid ? self.onValid() : self.onInvalid(); 804 self.afterValidation(); 805 806 if (self.valid) 807 ret = self.oldOnSubmit.call(this, e || window.event) !== false; 808 809 810 var parent = document.querySelector(".gform_body"); 811 var child = document.querySelector(".errorMessages"); 812 813 if (child) { 814 parent.removeChild(child); 815 } 816 817 if (show_error_combined == true) { 818 //showing error messages 819 var d = document.getElementsByClassName("LV_invalid_field"); 820 if (d.length > 0) { 821 822 823 var formIDConfig = self.name.split("_"); 824 var ul = document.createElement('ul'); 825 var section = document.querySelector(".gform_body"); 826 ul.className = "errorMessages"; 827 var contain_names = new Array(); 828 var containe_checks = new Array(); 829 section.insertBefore(ul, section.firstChild); 830 for (var i = 0; i < d.length; i++) { 831 832 833 var ID = d[i].id; 834 835 if (contain_names.indexOf(d[i].name) == -1) { 836 contain_names.push(d[i].name); 837 } 838 else { 839 continue; 840 } 841 842 843 if (d[i].type == "checkbox") { 844 845 846 var a = d[i].name; 847 var name = a.split(".")[0]; 848 849 if (containe_checks.indexOf(name) == -1) { 850 containe_checks.push(name); 851 } 852 else { 853 continue; 854 } 855 856 } 857 if (d[i].tagName == "UL") { 858 continue; 859 } 860 861 862 var error_message_elem = d[i].nextSibling; 863 var label_elem = document.querySelector(".gfield_label[for='" + ID + "']"); 864 if (!label_elem) { 865 866 label_elem = document.getElementById(ID + "_label"); 867 } 868 869 if (!label_elem) { 870 871 label_elem = document.querySelector("label[for='" + ID + "']"); 872 } 873 874 875 if (d[i].type == "radio") { 876 var get_mainLI = "field_" + formIDConfig[1] + "_" + d[i].name.substr(6); 877 878 label_elem = document.querySelector("#" + get_mainLI + " .gfield_label"); 879 880 } 881 if (d[i].type == "checkbox") { 882 var a = d[i].name; 883 var name = a.split(".")[0]; 884 885 var get_mainLI = "field_" + formIDConfig[1] + "_" + name.substr(6); 886 887 label_elem = document.querySelector("#" + get_mainLI + " .gfield_label"); 888 889 } 890 891 892 893 894 895 var li_elem = label_elem.parentNode; 896 897 898 899 if (moveTo == 0) { 900 moveTo = jqobjPrime(li_elem).offset().top; 901 } 902 var li = document.createElement('li'); 903 904 //ul.appendChild(li); 905 906 907 if (label_elem && error_message_elem) { 908 909 var error_message_text = (error_message_elem.textContent || error_message_elem.innerText); 910 var label_text1 = (label_elem.textContent || label_elem.innerText); 911 if (label_text1 == "" || label_text1 == "*") { 912 913 label_elem = document.querySelector("#" + ID + "_container label[for='" + ID + "']"); 914 var label_text1 = (label_elem.textContent || label_elem.innerText); 915 916 } 917 918 li.innerHTML = "<span><a href='#' onclick='window.scrollTo(0, " + jqobjPrime(li_elem).offset().top + "); return false;'>" + label_text1 + "</a></span> " + error_message_text + " "; 919 } 920 921 922 923 924 925 } 926 927 var ul_j = jqobjPrime('.errorMessages'); 928 929 930 moveTo = window.lv_offset(moveTo); 931 jqobjPrime('body,html').animate({ 932 scrollTop: moveTo 933 }, 500); 934 935 } 936 937 var elems = document.getElementsByClassName("LV_validation_message"); 938 939// if (elems.length > 0) { 940// for (var i = 0; i < elems.length; i++) { 941// var classes = elems[i].className; 942// elems[i].className = classes + " hide_validation"; 943// 944// } 945// } 946 947 948 } 949 950 var getFormIDconfig = self.name.split("_"); 951 952 953 if (false === ret) { 954 window['gf_submitting_' + getFormIDconfig[1]] = false; 955 } 956 if (!ret) { 957 if (lv_gf_is_ajax == "yes") { 958 jqobjPrime(".gform_ajax_spinner").remove(); 959 } 960 return ret; 961 } 962 963 } 964 }, 965 /** 966 * adds a LiveValidation field to the forms fields array 967 * 968 * @param element {LiveValidation} - a LiveValidation object 969 */ 970 addField: function(newField) { 971 this.fields.push(newField); 972 }, 973 /** 974 * removes a LiveValidation field from the forms fields array 975 * 976 * @param victim {LiveValidation} - a LiveValidation object 977 */ 978 removeField: function(victim) { 979 var victimless = []; 980 for (var i = 0, len = this.fields.length; i < len; i++) { 981 if (this.fields[i] !== victim) 982 victimless.push(this.fields[i]); 983 } 984 this.fields = victimless; 985 }, 986 /** 987 * destroy this instance and its events 988 * 989 * @param force {Boolean} - whether to force the detruction even if there are fields still associated 990 */ 991 destroy: function(force) { 992 // only destroy if has no fields and not being forced 993 if (this.fields.length != 0 && !force) 994 return false; 995 // remove events - set back to previous events 996 this.element.onsubmit = this.oldOnSubmit; 997 // remove from the instances namespace 998 LiveValidationForm.instances[this.name] = null; 999 return true; 1000 } 1001 1002} 1003 1004/*************************************** Validate class ****************************************/ 1005/** 1006 * This class contains all the methods needed for doing the actual validation itself 1007 * 1008 * All methods are static so that they can be used outside the context of a form field 1009 * as they could be useful for validating stuff anywhere you want really 1010 * 1011 * All of them will return true if the validation is successful, but will raise a ValidationError if 1012 * they fail, so that this can be caught and the message explaining the error can be accessed ( as just 1013 * returning false would leave you a bit in the dark as to why it failed ) 1014 * 1015 * Can use validation methods alone and wrap in a try..catch statement yourself if you want to access the failure 1016 * message and handle the error, or use the Validate::now method if you just want true or false 1017 */ 1018 1019var Validate = { 1020 /** 1021 * validates that the field has been filled in 1022 * 1023 * @param value {mixed} - value to be checked 1024 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1025 * 1026 * paramsObj properties:
1027 * failureMessage {String} - the message to show when the field fails validation 1028 * (DEFAULT: "Can't be empty!") 1029 */ 1030 Presence: function(value, paramsObj) { 1031 1032 1033 1034 var paramsObj = paramsObj || {}; 1035 var message = paramsObj.failureMessage || "Can't be empty!"; 1036 1037 if (typeof paramsObj.mask !== "undefined") { 1038 if (paramsObj.mask == value) { 1039 Validate.fail(message); 1040 } else { 1041 setTimeout(function() { 1042 1043 if (!hasClass(all_validations[paramsObj.form_id][paramsObj.livevalidkey].element, "LV_invalid_field")) { 1044 all_validations[paramsObj.form_id][paramsObj.livevalidkey].validate(); 1045 } 1046 1047 1048 }, 300); 1049 } 1050 1051 1052 } 1053 1054 if (value === '' || value === null || value === undefined || value == "First Name" || value == "Last Name" || value == "Middle Name") 1055 Validate.fail(message); 1056 return true; 1057 }, 1058 /** 1059 * validates that the value is numeric, does not fall within a given range of numbers 1060 * 1061 * @param value {mixed} - value to be checked 1062 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1063 * 1064 * paramsObj properties: 1065 * notANumberMessage {String} - the message to show when the validation fails when value is not a number 1066 * (DEFAULT: "Must be a number!") 1067 * notAnIntegerMessage {String} - the message to show when the validation fails when value is not an integer 1068 * (DEFAULT: "Must be a number!") 1069 * wrongNumberMessage {String} - the message to show when the validation fails when is param is used 1070 * (DEFAULT: "Must be {is}!") 1071 * tooLowMessage {String} - the message to show when the validation fails when minimum param is used 1072 * (DEFAULT: "Must not be less than {minimum}!") 1073 * tooHighMessage {String} - the message to show when the validation fails when maximum param is used 1074 * (DEFAULT: "Must not be more than {maximum}!") 1075 * is {Int} - the length must be this long 1076 * minimum {Int} - the minimum length allowed 1077 * maximum {Int} - the maximum length allowed 1078 * onlyInteger {Boolean} - if true will only allow integers to be valid 1079 * (DEFAULT: false) 1080 * 1081 * NB. can be checked if it is within a range by specifying both a minimum and a maximum 1082 * NB. will evaluate numbers represented in scientific form (ie 2e10) correctly as numbers 1083 */ 1084 Numericality: function(value, paramsObj) { 1085 var suppliedValue = value; 1086 var value = Number(value); 1087 var paramsObj = paramsObj || {}; 1088 var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null; 1089 ; 1090 var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null; 1091 var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null; 1092 var notANumberMessage = paramsObj.notANumberMessage || "Must be a number!"; 1093 var notAnIntegerMessage = paramsObj.notAnIntegerMessage || "Must be an integer!"; 1094 var wrongNumberMessage = paramsObj.wrongNumberMessage || "Must be " + is + "!"; 1095 var tooLowMessage = paramsObj.tooLowMessage || "Must not be less than " + minimum + "!"; 1096 var tooHighMessage = paramsObj.tooHighMessage || "Must not be more than " + maximum + "!"; 1097 if (!isFinite(value)) 1098 Validate.fail(notANumberMessage); 1099 if (paramsObj.onlyInteger && (/\.0+$|\.$/.test(String(suppliedValue)) || value != parseInt(value))) 1100 Validate.fail(notAnIntegerMessage); 1101 switch (true) { 1102 case (is !== null): 1103 if (value != Number(is)) 1104 Validate.fail(wrongNumberMessage); 1105 break; 1106 case (minimum !== null && maximum !== null): 1107 Validate.Numericality(value, { 1108 tooLowMessage: tooLowMessage, 1109 minimum: minimum 1110 }); 1111 Validate.Numericality(value, { 1112 tooHighMessage: tooHighMessage, 1113 maximum: maximum 1114 }); 1115 break; 1116 case (minimum !== null): 1117 if (value < Number(minimum)) 1118 Validate.fail(tooLowMessage); 1119 break; 1120 case (maximum !== null): 1121 if (value > Number(maximum)) 1122 Validate.fail(tooHighMessage); 1123 break; 1124 } 1125 return true; 1126 }, 1127 /** 1128 * validates against a RegExp pattern 1129 * 1130 * @param value {mixed} - value to be checked 1131 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1132 * 1133 * paramsObj properties:
1134 * failureMessage {String} - the message to show when the field fails validation 1135 * (DEFAULT: "Not valid!") 1136 * pattern {RegExp} - the regular expression pattern 1137 * (DEFAULT: /./) 1138 * negate {Boolean} - if set to true, will validate true if the pattern is not matched 1139 * (DEFAULT: false) 1140 * 1141 * NB. will return true for an empty string, to allow for non-required, empty fields to validate. 1142 * If you do not want this to be the case then you must either add a LiveValidation.PRESENCE validation 1143 * or build it into the regular expression pattern 1144 */ 1145 Format: function(value, paramsObj) { 1146 1147 1148 1149 1150 var value = String(value); 1151 var paramsObj = paramsObj || {}; 1152 var message = paramsObj.failureMessage || "Looks like an invalid pattern."; 1153 var pattern = paramsObj.pattern || /./; 1154 var negate = paramsObj.negate || false; 1155 1156 1157 1158 1159 var patternRes = pattern.test(value); 1160 1161 if (!negate && !patternRes) { 1162 1163 Validate.fail(message); // normal 1164 } 1165 1166 if (negate && patternRes) { 1167 Validate.fail(message); // negated 1168 } 1169 1170 1171 return true; 1172 }, 1173 /** 1174 * validates that the field contains a valid email address 1175 * 1176 * @param value {mixed} - value to be checked 1177 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1178 * 1179 * paramsObj properties: 1180 * failureMessage {String} - the message to show when the field fails validation 1181 * (DEFAULT: "Must be a number!" or "Must be an integer!") 1182 */ 1183 Email: function(value, paramsObj) { 1184 var paramsObj = paramsObj || {}; 1185 var message = paramsObj.failureMessage || "Must be a valid email address!"; 1186 Validate.Format(value, { 1187 failureMessage: message, 1188 pattern: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i 1189 }); 1190 return true; 1191 }, 1192 Website: function(value, paramsObj) { 1193 var paramsObj = paramsObj || {}; 1194 var message = paramsObj.failureMessage || "Must be a valid url!"; 1195 Validate.Format(value, { 1196 failureMessage: message, 1197 pattern: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ 1198 ///^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/ 1199 }); 1200 return true; 1201 }, 1202 Name: function(value, paramsObj) { 1203 var paramsObj = paramsObj || {}; 1204 var message = paramsObj.failureMessage || "only characters!"; 1205 1206 1207 Validate.Format(value, { 1208 failureMessage: message, 1209 pattern: /^[a-zA-Z]*$/ 1210 }); 1211 return true; 1212 }, 1213 Phone: function(value, paramsObj) { 1214 var paramsObj = paramsObj || {}; 1215 var formate = paramsObj.phoneFormate || ""; 1216 if (formate == 'standard') { 1217 var message = paramsObj.failureMessage || "Phone format:(###)###-####"; 1218 var phonefield = paramsObj.phonefield; 1219 var newnumber = ""; 1220 for (place = 0; place <= value.length; place++) 1221 newnumber = newnumber + value.charAt(place).replace(/\D*/, ''); 1222 if (newnumber.length == 10) { 1223 1224 // document.getElementById(phonefield).value = '(' + newnumber.substring(0, 3) + ')' + newnumber.substring(3, 6) + '-' + newnumber.substring(6, newnumber.length); 1225 return true; 1226 } 1227 else if (newnumber.length == 0) { 1228 Validate.fail(message); 1229 return false; 1230 } 1231 else { 1232 Validate.fail(message); 1233 return false; 1234 } 1235 } else { 1236 var message = paramsObj.failureMessage || "only numbers!"; 1237 Validate.Format(value, { 1238 failureMessage: message, 1239 pattern: /^[0-9()-]*$/ 1240 // /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/ 1241 // /^[0-9()-]*$/ 1242 }); 1243 } 1244 return true; 1245 }, 1246 /** 1247 * validates the length of the value 1248 * 1249 * @param value {mixed} - value to be checked 1250 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1251 * 1252 * paramsObj properties:
1253 * wrongLengthMessage {String} - the message to show when the fails when is param is used 1254 * (DEFAULT: "Must be {is} characters long!") 1255 * tooShortMessage {String} - the message to show when the fails when minimum param is used 1256 * (DEFAULT: "Must not be less than {minimum} characters long!") 1257 * tooLongMessage {String} - the message to show when the fails when maximum param is used 1258 * (DEFAULT: "Must not be more than {maximum} characters long!") 1259 * is {Int} - the length must be this long 1260 * minimum {Int} - the minimum length allowed 1261 * maximum {Int} - the maximum length allowed 1262 * 1263 * NB. can be checked if it is within a range by specifying both a minimum and a maximum 1264 */ 1265 Length: function(value, paramsObj) { 1266 var value = String(value); 1267 var paramsObj = paramsObj || {}; 1268 var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null; 1269 var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null; 1270 var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null; 1271 var wrongLengthMessage = paramsObj.wrongLengthMessage || "Must be " + is + " characters long!"; 1272 var tooShortMessage = paramsObj.tooShortMessage || "Must not be less than " + minimum + " characters long!"; 1273 var tooLongMessage = paramsObj.tooLongMessage || "Must not be more than " + maximum + " characters long!"; 1274 switch (true) { 1275 case (is !== null): 1276 if (value.length != Number(is)) 1277 Validate.fail(wrongLengthMessage); 1278 break; 1279 case (minimum !== null && maximum !== null): 1280 Validate.Length(value, { 1281 tooShortMessage: tooShortMessage, 1282 minimum: minimum 1283 }); 1284 Validate.Length(value, { 1285 tooLongMessage: tooLongMessage, 1286 maximum: maximum 1287 }); 1288 break; 1289 case (minimum !== null): 1290 if (value.length < Number(minimum)) 1291 Validate.fail(tooShortMessage); 1292 break; 1293 case (maximum !== null): 1294 if (value.length > Number(maximum)) 1295 Validate.fail(tooLongMessage); 1296 break; 1297 default: 1298 throw new Error("Validate::Length - Length(s) to validate against must be provided!"); 1299 } 1300 return true; 1301 }, 1302 /** 1303 * validates that the value falls within a given set of values 1304 * 1305 * @param value {mixed} - value to be checked 1306 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1307 * 1308 * paramsObj properties: 1309 * failureMessage {String} - the message to show when the field fails validation 1310 * (DEFAULT: "Must be included in the list!") 1311 * within {Array} - an array of values that the value should fall in 1312 * (DEFAULT: []) 1313 * allowNull {Bool} - if true, and a null value is passed in, validates as true 1314 * (DEFAULT: false) 1315 * partialMatch {Bool} - if true, will not only validate against the whole value to check but also if it is a substring of the value 1316 * (DEFAULT: false) 1317 * caseSensitive {Bool}
1317 - if false will compare strings case insensitively 1318 * (DEFAULT: true) 1319 * negate {Bool} - if true, will validate that the value is not within the given set of values 1320 * (DEFAULT: false) 1321 */ 1322 Inclusion: function(value, paramsObj) { 1323 var paramsObj = paramsObj || {}; 1324 var message = paramsObj.failureMessage || "Must be included in the list!"; 1325 var caseSensitive = (paramsObj.caseSensitive === false) ? false : true; 1326 if (paramsObj.allowNull && value == null) 1327 return true; 1328 if (!paramsObj.allowNull && value == null) 1329 Validate.fail(message); 1330 var within = paramsObj.within || []; 1331 //if case insensitive, make all strings in the array lowercase, and the value too 1332 if (!caseSensitive) { 1333 var lowerWithin = []; 1334 for (var j = 0, length = within.length; j < length; ++j) { 1335 var item = within[j]; 1336 if (typeof item == 'string') 1337 item = item.toLowerCase(); 1338 lowerWithin.push(item); 1339 } 1340 within = lowerWithin; 1341 if (typeof value == 'string') 1342 value = value.toLowerCase(); 1343 } 1344 var found = false; 1345 for (var i = 0, length = within.length; i < length; ++i) { 1346 if (within[i] == value) 1347 found = true; 1348 if (paramsObj.partialMatch) { 1349 if (value.indexOf(within[i]) != -1) 1350 found = true; 1351 } 1352 } 1353 if ((!paramsObj.negate && !found) || (paramsObj.negate && found)) 1354 Validate.fail(message); 1355 return true; 1356 }, 1357 /** 1358 * validates that the value does not fall within a given set of values 1359 * 1360 * @param value {mixed} - value to be checked 1361 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1362 * 1363 * paramsObj properties: 1364 * failureMessage {String} - the message to show when the field fails validation 1365 * (DEFAULT: "Must not be included in the list!") 1366 * within {Array} - an array of values that the value should not fall in 1367 * (DEFAULT: []) 1368 * allowNull {Bool} - if true, and a null value is passed in, validates as true 1369 * (DEFAULT: false) 1370 * partialMatch {Bool} - if true, will not only validate against the whole value to check but also if it is a substring of the value 1371 * (DEFAULT: false) 1372 * caseSensitive {Bool}
1372 - if false will compare strings case insensitively 1373 * (DEFAULT: true) 1374 */ 1375 Exclusion: function(value, paramsObj) { 1376 var paramsObj = paramsObj || {}; 1377 paramsObj.failureMessage = paramsObj.failureMessage || "Must not be included in the list!"; 1378 paramsObj.negate = true; 1379 Validate.Inclusion(value, paramsObj); 1380 return true; 1381 }, 1382 /** 1383 * validates that the value matches that in another field 1384 * 1385 * @param value {mixed} - value to be checked 1386 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1387 * 1388 * paramsObj properties: 1389 * failureMessage {String} - the message to show when the field fails validation 1390 * (DEFAULT: "Does not match!") 1391 * match {String} - id of the field that this one should match 1392 */ 1393 Confirmation: function(value, paramsObj) { 1394 if (!paramsObj.match) 1395 throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!"); 1396 var paramsObj = paramsObj || {}; 1397 var message = paramsObj.failureMessage || "Does not match!"; 1398 var match = paramsObj.match.nodeName ? paramsObj.match : document.getElementById(paramsObj.match); 1399 if (!match) 1400 throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '" + paramsObj.match + "'!"); 1401 if (value != match.value) 1402 Validate.fail(message); 1403 return true; 1404 }, 1405 /** 1406 * validates that the value is true (for use primarily in detemining if a checkbox has been checked) 1407 * 1408 * @param value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox) 1409 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1410 * 1411 * paramsObj properties: 1412 * failureMessage {String} - the message to show when the field fails validation 1413 * (DEFAULT: "Must be accepted!") 1414 */ 1415 Acceptance: function(value, paramsObj) { 1416 var paramsObj = paramsObj || {}; 1417 var message = paramsObj.failureMessage || "Must be accepted!"; 1418 if (!value) 1419 Validate.fail(message); 1420 return true; 1421 }, 1422 ConfirmEmail: function(value, paramsObj) { 1423 1424 var paramsObj = paramsObj || {}; 1425 var message = paramsObj.failureMessage || "Must be accepted!"; 1426 console.log('runs'); 1427 var ParentValue = document.getElementById(paramsObj.parentField).value; 1428 console.log(value); 1429 console.log(ParentValue); 1430 1431 1432 if (value !== ParentValue) { 1433 Validate.fail(message); 1434 } 1435 1436 return true; 1437 }, 1438 AcceptanceCheckbox: function(value, paramsObj) { 1439 1440 var paramsObj = paramsObj || {}; 1441 var message = paramsObj.failureMessage || "Must be accepted!"; 1442 1443 var get_mainUL = "input_" + paramsObj.form_id + "_" + paramsObj.field_id; 1444 1445 var radioUL = document.getElementById(get_mainUL); 1446 1447 var len = radioUL.childNodes.length; 1448 for (var i = 0; i < radioUL.childNodes.length; i++) { 1449 if (radioUL.childNodes[i].querySelector('input').checked) { 1450 if (typeof radioUL.classList == "undefined") { 1451 removeClass(radioUL, 'LV_invalid_field'); 1452 } else { 1453 radioUL.classList.remove("LV_invalid_field"); 1454 } 1455 1456 1457 1458 1459 for (var j = 0; j < radioUL.childNodes.length; j++) { 1460 removeClass(radioUL.childNodes[j].querySelector('input'), 'LV_invalid_field'); 1461 } 1462 1463 return true; 1464 } 1465 1466 } 1467 1468 1469 1470 1471 if (!value) { 1472 1473 if (typeof radioUL.classList == "undefined") { 1474 addClass(radioUL, 'LV_invalid_field'); 1475 } else {
1476 radioUL.classList.add("LV_invalid_field"); 1477 } 1478 1479 Validate.fail(message); 1480 1481 } 1482 else { 1483 if (typeof radioUL.classList == "undefined") { 1484 removeClass(radioUL, 'LV_invalid_field'); 1485 } else { 1486 radioUL.classList.remove("LV_invalid_field"); 1487 } 1488 } 1489 return true; 1490 }, 1491 //Acceptance validation function only for radio buttons 1492 AcceptanceRadio: function(value, paramsObj) { 1493 1494 1495 1496 1497 var returnVar = false; 1498 var paramsObj = paramsObj || {}; 1499 var message = paramsObj.failureMessage || "Must be accepted!"; 1500 1501 var get_mainUL = "input_" + paramsObj.form_id + "_" + paramsObj.name_field.substr(6); 1502 1503 var radioUL = document.getElementById(get_mainUL); 1504 if (paramsObj.name_field) { 1505 var inps = document.getElementsByName(paramsObj.name_field); 1506 for (var i = 0; i < inps.length; i++) { 1507 1508 if (inps[i].checked) { 1509 if (typeof radioUL.classList == "undefined") { 1510 removeClass(radioUL, 'LV_invalid_field'); 1511 } else { 1512 radioUL.classList.remove("LV_invalid_field"); 1513 } 1514 1515 1516 if (typeof radioUL.classList == "undefined") { 1517 removeClass(radioUL, 'LV_invalid_field'); 1518 var inps = document.getElementsByName(paramsObj.name_field); 1519 for (var i = 0; i < inps.length; i++) { 1520 1521 removeClass(inps[i], 'LV_invalid_field'); 1522 } 1523 } else { 1524 radioUL.classList.remove("LV_invalid_field"); 1525 1526 } 1527 var inps = document.getElementsByName(paramsObj.name_field); 1528 for (var i = 0; i < inps.length; i++) { 1529 1530 removeClass(inps[i], 'LV_invalid_field'); 1531 } 1532 1533 return true; 1534 } 1535 } 1536 } 1537 1538 1539 if (!value) { 1540 1541 1542 1543 1544 1545 if (typeof radioUL.classList == "undefined") { 1546 addClass(radioUL, 'LV_invalid_field'); 1547 } else { 1548 radioUL.classList.add("LV_invalid_field"); 1549 } 1550 1551 1552 1553 1554 1555 Validate.fail(message); 1556 1557 } 1558 else { 1559 if (typeof radioUL.classList == "undefined") { 1560 removeClass(radioUL, 'LV_invalid_field'); 1561 var inps = document.getElementsByName(paramsObj.name_field); 1562 for (var i = 0; i < inps.length; i++) { 1563 1564 removeClass(inps[i], 'LV_invalid_field'); 1565 } 1566 } else { 1567 radioUL.classList.remove("LV_invalid_field"); 1568 1569 } 1570 var inps = document.getElementsByName(paramsObj.name_field); 1571 for (var i = 0; i < inps.length; i++) { 1572 1573 removeClass(inps[i], 'LV_invalid_field'); 1574 } 1575 } 1576 return true; 1577 1578 }, 1579 /** 1580 * validates against a custom function that returns true or false (or throws a Validate.Error) when passed the value 1581 * 1582 * @param value {mixed} - value to be checked 1583 * @param paramsObj {Object} - parameters for this particular validation, see below for details 1584 * 1585 * paramsObj properties: 1586 * failureMessage {String} - the message to show when the field fails validation 1587 * (DEFAULT: "Not valid!") 1588 * against {Function} - a function that will take the value and object of arguments and return true or false 1589 * (DEFAULT: function(value, argsObj){ return true; }) 1590 * args {Object} - an object of named arguments that will be passed to the custom function so are accessible through this object within it 1591 * (DEFAULT: {}) 1592 */ 1593 Custom: function(value, paramsObj) { 1594 var paramsObj = paramsObj || {}; 1595 var against = paramsObj.against || function() { 1596 return true; 1597 }; 1598 var args = paramsObj.args || {};
1599 var message = paramsObj.failureMessage || "Not valid!"; 1600 if (!against(value, args)) 1601 Validate.fail(message); 1602 return true; 1603 }, 1604 GFCheckboxes: function(value, paramsObj) { 1605 1606 1607 1608 1609 var paramsObj = paramsObj || {}; 1610 var message = paramsObj.failureMessage || "Must be accepted!"; 1611 var isValidated = false; 1612 1613 1614 var radioUL = document.getElementById(paramsObj.field_name); 1615 1616 var len = radioUL.childNodes.length; 1617 for (var i = 0; i < radioUL.childNodes.length; i++) { 1618 1619 1620 if (radioUL.childNodes[i].querySelector('input').checked) { 1621 1622 1623 isValidated = true; 1624 return true; 1625 } 1626 1627 } 1628 1629 1630 1631 if (!isValidated) { 1632 1633 1634// 1635// 1636// if (typeof radioUL.classList == "undefined") { 1637// addClass(radioUL, 'LV_invalid_field'); 1638// } else { 1639// radioUL.classList.add("LV_invalid_field"); 1640// } 1641 1642 Validate.fail(message); 1643 1644// } 1645// else { 1646// if (typeof radioUL.classList == "undefined") { 1647// removeClass(radioUL, 'LV_invalid_field'); 1648// } else { 1649// radioUL.classList.remove("LV_invalid_field"); 1650// } 1651// } 1652 //return true; 1653 1654 1655 } 1656 }, 1657 Date: function(value, paramsObj) { 1658 1659 1660 var paramsObj = paramsObj || {}; 1661 var message = paramsObj.failureMessage || "Must be accepted!"; 1662 if (!value) 1663 Validate.fail(message); 1664 1665 var d = new Date(value); 1666 var current = new Date(); 1667 1668 if (Object.prototype.toString.call(d) === "[object Date]") { 1669 // it is a date 1670 if (isNaN(d.getTime())) { // d.valueOf() could also work 1671 Validate.fail(message); 1672 } 1673 else { 1674 1675 var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/; 1676 var regs = new Array(); 1677 if (regs = value.match(re)) { 1678 1679 if (regs[3] < (current.getFullYear() - 100)) { 1680 Validate.fail(message); 1681 } 1682 if (checkDay(regs[2], regs[1], regs[3])) { 1683 return true; 1684 } else { 1685 Validate.fail(message); 1686 } 1687 1688 1689 } 1690 1691 1692 Validate.fail(message); 1693 } 1694 } 1695 else { 1696 Validate.fail(message); 1697 } 1698 return true; 1699 }, 1700 /** 1701 * validates whatever it is you pass in, and handles the validation error for you so it gives a nice true or false reply 1702 * 1703 * @param validationFunction {Function} - validation function to be used (ie Validation.validatePresence ) 1704 * @param value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox) 1705 * @param validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary 1706 */ 1707 now: function(validationFunction, value, validationParamsObj) { 1708 if (!validationFunction) 1709 throw new Error("Validate::now - Validation function must be provided!"); 1710 var isValid = true; 1711 try { 1712 validationFunction(value, validationParamsObj || {}); 1713 } catch (error) { 1714 if (error instanceof Validate.Error) { 1715 isValid = false; 1716 } else { 1717 throw error; 1718 } 1719 } finally { 1720 return isValid 1721 } 1722 }, 1723 /** 1724 * shortcut for failing throwing a validation error 1725 * 1726 * @param errorMessage {String} - message to display 1727 */ 1728 fail: function(errorMessage) { 1729 throw new Validate.Error(errorMessage); 1730 }, 1731 Error: function(errorMessage) { 1732 this.message = errorMessage; 1733 this.name = 'ValidationError'; 1734 } 1735 1736} 1737 1738function phoneformat(fld) { 1739 var newnumber = ""; 1740 phonenumber = fld.val(); 1741 for (place = 0; place <= fld.val().length; place++) 1742 newnumber = newnumber + phonenumber.charAt(place).replace(/\D*/, ''); 1743 if (newnumber.length == 10) { 1744 fld.val('(' + newnumber.substring(0, 3) + ')' + newnumber.substring(3, 6) + '-' + newnumber.substring(6, newnumber.length)); 1745 alert(phonenumber); 1746 return true; 1747 } 1748 else if (newnumber.length == 0) { 1749 fld.val(''); 1750 // $('input[name="phone"]').parent().children('.error').html('Phone format: (###)###-#### <br/> This field is required.'); 1751 return false; 1752 } 1753 else { 1754 // $('input[name="phone"]').parent().children('.error').html('Phone format: (###)###-####'); 1755 fld.select(); 1756 // alert('Invalid Phone Number must be 10 digit number'); 1757 return false; 1758 } 1759} 1760function hasClass(ele, cls) { 1761 return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); 1762} 1763 1764function addClass(ele, cls) { 1765 if (!hasClass(ele, cls)) 1766 ele.className += " " + cls; 1767} 1768 1769function removeClass(ele, cls) { 1770 if (hasClass(ele, cls)) { 1771 var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); 1772 ele.className = ele.className.replace(reg, ' '); 1773 } 1774} 1775
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.