1//form-ajax.js 2(function() { 3 // counter gets updated on every invalid input and will focus only on the first input if counter is 1 4 // counter gets reset when submit is clicked 5 let counter = 1; 6 function focusInvalidInput(input) { 7 if(counter === 1) { 8 input.focus(); 9 } 10 counter++; 11 }; 12 13 (function ($) { 14 if (typeof window.FormData === 'undefined') 15 return; 16 17 $(function () { 18 var formContainers = $('[data-sf-role="form-container"]:has([data-sf-role="ajax-submit-url"])'); 19 formContainers.each(function (i, element) { 20 var formContainer = $(element); 21 var loadingImg = formContainer.find('[data-sf-role="loading-img"]'); 22 var fieldsContainer = formContainer.find('[data-sf-role="fields-container"]'); 23 var successMessage = formContainer.find('[data-sf-role="success-message"]'); 24 var errorMessage = formContainer.find('[data-sf-role="error-message"]'); 25 var generalErrorMessage = formContainer.find('[data-sf-role="general-error-message"]'); 26 var redirectUrl = formContainer.find('input[data-sf-role="redirect-url"]').val(); 27 var submitUrl = formContainer.find('input[data-sf-role="ajax-submit-url"]').val(); 28 29 var submitClickHandler = function () { 30 var parentForm = formContainer.closest('form'); 31 var parentFormChildren = parentForm.children(); 32 33 if (parentForm.length > 0) 34 parentFormChildren.unwrap(); 35 36 var newForm = formContainer.find('form'); 37 var wrapped = false; 38 if (newForm.length === 0) { 39 wrapped = true; 40 formContainer.wrap('<form />'); 41 newForm = formContainer.parent(); 42 } 43 44 newForm.one('submit', function () { 45 var inputs = formContainer.find('input'); 46 var isValid = true; 47 for (var i = 0; i < inputs.length; i++) { 48 var jInput = $(inputs[i]); 49 if (typeof (jInput.data('sfvalidator')) === 'function') 50 isValid = jInput.data('sfvalidator')() && isValid; 51 } 52 53 if (!isValid) 54 return false; 55 56 if (typeof MarketoSubmitScript !== 'undefined') { 57 MarketoSubmitScript._populateFormId($(newForm).find('input[data-sf-role="form-id"]').val()); 58 var newSubmitButtons = $(newForm).find(MarketoSubmitScript._settings.externalFormSubmitButtonsQuery); 59 if (newSubmitButtons.length > 0) 60 MarketoSubmitScript._formFields = Mar
60ketoSubmitScript._getExternalFormFields(newSubmitButtons[0]); 61 if (MarketoSubmitScript._formFields && MarketoSubmitScript._formFields.length === 0) 62 MarketoSubmitScript._populateFieldsFromLabels(newForm); 63 MarketoSubmitScript._formSubmitHandler(newForm); 64 } 65 66 var formData = new FormData(newForm[0]); 67 var request = new XMLHttpRequest(); 68 request.open('POST', submitUrl); 69 request.onload = function () { 70 if (request.status === 200) { 71 var responseJson = JSON.parse(request.response); 72 if (responseJson.success) { 73 if (responseJson.redirectUrl && responseJson.redirectUrl !== '') { 74 document.location.replace(responseJson.redirectUrl); 75 } else if (responseJson.message && responseJson.message !== '') { 76 successMessage.text(responseJson.message); 77 successMessage.show(); 78 loadingImg.hide(); 79 } else { 80 if (redirectUrl) { 81 document.location.replace(redirectUrl); 82 } 83 else { 84 successMessage.show(); 85 loadingImg.hide(); 86 } 87 } 88 } 89 else { 90 generalErrorMessage.text(responseJson.error); 91 generalErrorMessage.show(); 92 fieldsContainer.show(); 93 fieldsContainer.find('[data-sf-role="captcha-refresh-button"]').click(); 94 loadingImg.hide(); 95 } 96 } 97 }; 98 99 loadingImg.show(); 100 fieldsContainer.hide(); 101 errorMessage.hide(); 102 generalErrorMessage.hide(); 103 104 request.send(formData); 105 106 if (wrapped) 107 formContainer.unwrap(); 108 109 if (parentForm.length > 0) 110 parentFormChildren.wrapAll(parentForm); 111 112 return false; 113 }); 114 }; 115 116 formContainer.find('button[type="submit"],input[type="submit"]').click(submitClickHandler); 117 }); 118 }); 119 })(jQuery); 120 121 //captcha.js 122 (function ($) { 123 'use strict'; 124 125 /* 126 Rest Api 127 */ 128 var CaptchaRestApi = function (rootUrl) { 129 this.rootUrl = (rootUrl && rootUrl[rootUrl.length - 1] !== '/') ? (rootUrl + '/') : rootUrl; 130 }; 131 132 CaptchaRestApi.prototype = { 133 makeAjax: function (url, type, data) { 134 var options = { 135 type: type || 'GET', 136 url: url, 137 contentType: 'application/json', 138 accepts: { 139 text: 'application/json' 140 }, 141 cache: false 142 }; 143 144 if (data) { 145 options.data = JSON.stringify(data); 146 } 147 148 return $.ajax(options); 149 }, 150 151 getCaptcha: function () { 152 var getCaptchaUrl = this.rootUrl; 153 return this.makeAjax(getCaptchaUrl); 154 }, 155 156 validateCaptcha: function (data) {
157 var validateCaptchaUrl = this.rootUrl; 158 return this.makeAjax(validateCaptchaUrl, 'POST', data); 159 } 160 }; 161 162 /* 163 Widget 164 */ 165 var CaptchaWidget = function (wrapper, settings, resources) { 166 this.wrapper = wrapper; 167 this.settings = settings; 168 this.resources = resources; 169 }; 170 171 CaptchaWidget.prototype = { 172 /* 173 Elements 174 */ 175 getOrInitializeProperty: function (property, sfRole) { 176 if (!this[property]) { 177 this[property] = this.getElementByDataSfRole(sfRole); 178 } 179 180 return this[property]; 181 }, 182 getElementByDataSfRole: function (sfRole) { 183 return this.wrapper.find('[data-sf-role="' + sfRole + '"]'); 184 }, 185 captchaImage: function () { return this.getOrInitializeProperty('_captchaImage', 'captcha-image'); }, 186 captchaAudio: function () { return this.getOrInitializeProperty('_captchaAudio', 'captcha-audio'); }, 187 captchaAudioBtn: function () { return this.getOrInitializeProperty('_captchaAudioBtn', 'captcha-audio-btn'); }, 188 captchaInput: function () { return this.getOrInitializeProperty('_captchaInput', 'captcha-input'); }, 189 captchaRefreshLink: function () { return this.getOrInitializeProperty('_captchaRefreshLink', 'captcha-refresh-button'); }, 190 captchaDataKey: function () { return this.getOrInitializeProperty('_captchaDataKey', 'captcha-k'); }, 191 captchaDataInvalidAnswerMessage: function () { return this.getOrInitializeProperty('_captchaDataInvalidAnswerMessage', 'captcha-iam'); }, 192 errorMessage: function () { return this.getOrInitializeProperty('_errorMessage', 'error-message'); }, 193 194 /* 195 Logic 196 */ 197 captchaRefresh: function () { 198 var self = this; 199 var deferred = $.Deferred(); 200 201 self.captchaImage().attr("src", ""); 202 self.captchaInput().hide(); 203 204 self.restApi.getCaptcha().then(function (data) { 205 if (data) { 206 self.captchaImage().attr("src", "data:image/png;base64," + data.Image); 207 self.captchaAudio().attr("src", "data:audio/wav;base64," + data.Audio); 208 self.captchaAudioBtn().click(function () { self.captchaAudio()[0].play(); }); 209 self.captchaDataKey().val(data.Key); 210 self.captchaInput().val(""); 211 self.captchaInput().show(); 212 self.wrapper.show(); 213 } 214 215 deferred.resolve(true); 216 }); 217 218 return deferred; 219 }, 220 221 validateInput: function () { 222 var self = this; 223 var deferred = $.Deferred(), 224 data = { 225 Answer: self.captchaInput().val(), 226 Key: self.captchaDataKey().val() 227 }; 228 229 self.restApi.validateCaptcha(data).then(function (validationResult) { 230 if (validationResult.IsValid) { 231 self.hideInvalidMessage(); 232 } else { 233 self.showInvalidMessage(); 234 if (validationResult.RefreshCaptcha) { 235 self.captchaRefresh(); 236 } else { 237 self.wrapper.find('input').focus(); 238 self.wrapper.find('input').select(); 239 } 240 } 241 242 deferred.resolve(validationResult.IsValid); 243 }); 244 245 return deferred; 246 }, 247 248 /* 249 Initialization 250 */ 251 initializeProperties: function () { 252 this.restApi = new CaptchaRestApi(this.settings.rootUrl); 253 254 this.captchaData = { 255 key: null 256 }; 257 }, 258 259 initializeCaptcha: function () { 260 this.captchaRefresh();
261 }, 262 263 initializeHandlers: function () { 264 var self = this; 265 266 self.captchaRefreshLink().click(function () { 267 self.captchaRefresh(); 268 return false; 269 }); 270 }, 271 272 showInvalidMessage: function () { 273 // If there is no error message container the old logic is required 274 if (this.errorMessage().length > 0) { 275 var messasge = this.captchaDataInvalidAnswerMessage().val(); 276 this.errorMessage().text(messasge); 277 this.errorMessage().show(); 278 } else { 279 this.getElementByDataSfRole('invalid-captcha-input').css('visibility', 'visible'); 280 } 281 }, 282 283 hideInvalidMessage: function () { 284 // If there is no error message container the old logic is required 285 if (this.errorMessage().length > 0) { 286 this.errorMessage().hide(); 287 } else { 288 this.getElementByDataSfRole('invalid-captcha-input').css('visibility', 'hidden'); 289 } 290 }, 291 292 initialize: function () { 293 this.initializeProperties(); 294 this.initializeCaptcha(); 295 this.initializeHandlers(); 296 this.hideInvalidMessage(); 297 } 298 }; 299
300 function getValidationMessages(input) { 301 var container = $(input).parents('[data-sf-role="field-captcha-container"]'); 302 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 303 var validationMessages = JSON.parse(validationMessagesInput.val()); 304 305 return validationMessages; 306 } 307 308 function setErrorMessage(input, message) { 309 var errorMessagesContainer = _getErrorMessageContainer(input); 310 focusInvalidInput(input); 311 312 if (errorMessagesContainer) { 313 _toggleCustomErrorMessage(errorMessagesContainer, message); 314 } else { 315 input.setCustomValidity(message); 316 } 317 } 318 319 function _toggleCustomErrorMessage(container, message) { 320 container.innerText = message; 321 322 if (message === '') { 323 container.style.display = 'none'; 324 } else { 325 container.style.display = 'block'; 326 } 327 } 328 329 function _getErrorMessageContainer(input) { 330 var container = $(input).closest('[data-sf-role="field-captcha-container"]')[0]; 331 if (container) { 332 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 333 return errorMessagesContainer; 334 } 335 336 return null; 337 } 338 339 function changeOrInput(e) { 340 if (typeof e.target.validity == 'undefined') 341 return; 342 343 if (e.target.required && e.target.validity.valueMissing) { 344 var validationMessages = getValidationMessages(e.target); 345 setErrorMessage(e.target, validationMessages.required); 346 } else { 347 setErrorMessage(e.target, ''); 348 } 349 350 // If there is no error message container the old logic is required 351 if (!_getErrorMessageContainer(e.target)) { 352 e.data.hideInvalidMessage(); 353 } 354 } 355
356 function invalid(e) { 357 if (typeof e.target.validity == 'undefined') 358 return; 359 360 if (_getErrorMessageContainer(e.target)) { 361 e.preventDefault(); 362 } 363 364 var validationMessages = getValidationMessages(e.target); 365 366 if (e.target.validity.valueMissing) { 367 setErrorMessage(e.target, validationMessages.required); 368 } 369 } 370 371 /* 372 Widget creation 373 */ 374 $(function () { 375 $('[data-sf-role="field-captcha-container"]').each(function () { 376 var container = $(this); 377 var rootUrl = container.find('[data-sf-role="captcha-settings"]').val(); 378 var captcha = new CaptchaWidget(container, { rootUrl: rootUrl }); 379 captcha.initialize(); 380 381 var input = container.find('[data-sf-role="captcha-input"]'); 382 383 if (input) { 384 input.on('change', captcha, changeOrInput); 385 input.on('input', captcha, changeOrInput); 386 input.on('invalid', invalid); 387 input.data('widget-validator', function () { 388 return captcha.validateInput(); 389 }); 390 } 391 }); 392 }); 393 }(jQuery)); 394 //checkboxes-field.js 395 (function ($) { 396 var changeOrInput = function (e) { 397 var container = $(e.target).parents('[data-sf-role="checkboxes-field-container"]'); 398 var inputs = $(container).find('[data-sf-role="checkboxes-field-input"]'); 399 var hasChecked = $(container).find('input[data-sf-role="checkboxes-field-input"]:checked').length > 0; 400 401 if (hasChecked || container.find('[data-sf-role="required-validator"]').val() === 'False') { 402 $(inputs[0]).removeAttr('required'); 403 setErrorMessage(inputs[0], ''); 404 } 405 else { 406 $(inputs[0]).attr('required', 'required'); 407 } 408 }; 409 410 var invalid = function (e) { 411 var validationMessages = getValidationMessages(e.target); 412 413 if (_getErrorMessageContainer(e.target)) { 414 e.preventDefault(); 415 } 416 417 if (e.target.validity.valueMissing) { 418 setErrorMessage(e.target, validationMessages.required); 419 } 420 }; 421 422 var getValidationMessages = function (input) { 423 var container = $(input).parents('[data-sf-role="checkboxes-field-container"]'); 424 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 425 var validationMessages = JSON.parse(validationMessagesInput.val()); 426 427 return validationMessages; 428 }; 429 430 var setErrorMessage = function (input, message) { 431 var errorMessagesContainer = _getErrorMessageContainer(input); 432 focusInvalidInput(input); 433 434 if (errorMessagesContainer) { 435 _toggleCustomErrorMessage(errorMessagesContainer, message); 436 } else { 437 input.setCustomValidity(message); 438 } 439 }; 440 441 var _toggleCustomErrorMessage = function (container, message) { 442 container.innerText = message; 443 444 if (message === '') { 445 container.style.display = 'none'; 446 } else { 447 container.style.display = 'block'; 448 } 449 }; 450 451 var _getErrorMessageContainer = function (input) { 452 var container = $(input).closest('[data-sf-role="checkboxes-field-container"]')[0]; 453 if (container) { 454 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 455 return errorMessagesContainer; 456 } 457 458 return null; 459 }; 460 461 $(function () { 462 var containers = $('[data-sf-role="checkboxes-field-container"]'); 463 464 if (!containers || containers.length < 1) 465 return; 466 467 var attachHandlers = function (input) { 468 input.on('change', function (e) { 469 changeOrInput(e); 470 if (typeof $.fn.processFormRules == 'function') { 471 $(e.target).processFormRules();
472 } 473 }); 474 input.on('input', changeOrInput); 475 input.on('invalid', invalid); 476 }; 477 478 var checkboxClickHandler = function (e) { 479 var container = $(e.target).parents('[data-sf-role="checkboxes-field-container"]'); 480 var checkboxes = container.find('input[data-sf-role="checkboxes-field-input"]'); 481 var otherInput = $(container.find('[data-sf-checkboxes-role="other-choice-text"]').first()); 482 var otherCheckbox = $(container.find('[data-sf-checkboxes-role="other-choice-checkbox"]').first()); 483 var otherCheckboxIndex = checkboxes.index(otherCheckbox); 484 var currentIndex = checkboxes.index($(e.target)); 485 var isRequired = container.find('[data-sf-role="required-validator"]').val() === 'True'; 486 487 if (otherCheckbox.is(':checked')) { 488 otherInput.attr('type', 'text'); 489 490 if (isRequired) 491 otherInput.attr('required', 'required'); 492 else 493 otherInput.removeAttr('required'); 494 } 495 else { 496 otherInput.attr('type', 'hidden'); 497 otherInput.removeAttr('required'); 498 } 499 }; 500 501 var inputChangeHandler = function (e) { 502 var container = $(e.target).parents('[data-sf-role="checkboxes-field-container"]'); 503 var otherCheckbox = $(container.find('[data-sf-checkboxes-role="other-choice-checkbox"]').first()); 504 otherCheckbox.val($(e.target).val()); 505 506 if (typeof $.fn.processFormRules == 'function') { 507 $(e.target).processFormRules(); 508 } 509 }; 510 511 for (var i = 0; i < containers.length; i++) { 512 var container = $(containers[i]); 513 var inputs = container.find('[data-sf-role="checkboxes-field-input"]'); 514 515 if (container.find('[data-sf-role="required-validator"]').val() === 'True' && !inputs.is(':checked')) 516 $(inputs[0]).attr('required', 'required'); 517 518 attachHandlers(inputs); 519 520 var checkboxes = container.find('input[data-sf-role="checkboxes-field-input"]'); 521 522 var otherInput = $(container.find('[data-sf-checkboxes-role="other-choice-text"]').first()); 523 524 checkboxes.click(checkboxClickHandler); 525 otherInput.change(inputChangeHandler); 526 } 527 }); 528 }(jQuery)); 529 //dropdown-list-field.js 530 (function ($) { 531 $(function () { 532 function changeOrInput(e) { 533 if (e.target.value === '') { 534 var validationMessages = getValidationMessages(e.target); 535 setErrorMessage(e.target, validationMessages.required); 536 } 537 else { 538 setErrorMessage(e.target, ''); 539 } 540 541 if (typeof $.fn.processFormRules == 'function') { 542 $(e.target).processFormRules(); 543 } 544 } 545
546 function invalid(e) { 547 var validationMessages = getValidationMessages(e.target); 548 549 if (_getErrorMessageContainer(e.target)) { 550 e.preventDefault(); 551 } 552 553 if (e.target.validity.valueMissing) { 554 setErrorMessage(e.target, validationMessages.required); 555 } 556 } 557 558 function getValidationMessages(input) { 559 var container = $(input).parents('[data-sf-role="dropdown-list-field-container"]'); 560 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 561 var validationMessages = JSON.parse(validationMessagesInput.val()); 562 563 return validationMessages; 564 } 565 566 function setErrorMessage(input, message) { 567 var errorMessagesContainer = _getErrorMessageContainer(input); 568 focusInvalidInput(input); 569 570 if (errorMessagesContainer) { 571 _toggleCustomErrorMessage(errorMessagesContainer, message); 572 } else { 573 input.setCustomValidity(message); 574 } 575 } 576 577 function _toggleCustomErrorMessage(container, message) { 578 container.innerText = message; 579 580 if (message === '') { 581 container.style.display = 'none'; 582 } else { 583 container.style.display = 'block'; 584 } 585 } 586 587 function _getErrorMessageContainer(input) { 588 var container = $(input).closest('[data-sf-role="dropdown-list-field-container"]')[0]; 589 if (container) { 590 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 591 return errorMessagesContainer; 592 } 593 594 return null; 595 } 596 597 function init() { 598 var containers = $('[data-sf-role="dropdown-list-field-container"]'); 599 600 if (!containers || containers.length < 1) 601 return; 602 603 for (var i = 0; i < containers.length; i++) { 604 var select = $(containers[i]).find('[data-sf-role="dropdown-list-field-select"]'); 605 606 if (select) { 607 select.on('change', changeOrInput); 608 select.on('invalid', invalid); 609 } 610 } 611 } 612 613 init(); 614 }); 615 }(jQuery)); 616 //file-field.js 617 (function ($) { 618 var htmlDecode = function (html) { 619 var a = document.createElement('a'); a.innerHTML = html; 620 return a.textContent; 621 }; 622 623 var adjustVisibility = function (container) { 624 var allRemoveLinks = container.find('[data-sf-role="remove-input"]'); 625 allRemoveLinks.toggle(allRemoveLinks.length > 1); 626 }; 627 628 var initInput = function (template, container, config) { 629 var input = $(template); 630 input.appendTo(container); 631 632 input.on('change', function (e) { 633 if (typeof $.fn.processFormRules === 'function') { 634 $(e.target).processFormRules(); 635 } 636 }); 637 638 if (config.AllowMultipleFiles) { 639 adjustVisibility(container); 640 641 input.find('[data-sf-role="remove-input"]').click(function () { 642 input.remove(); 643 adjustVisibility(container); 644 if (typeof $.fn.processFormRules === 'function') { 645 $(container).processFormRules(); 646 } 647 }); 648 } 649 }; 650 651 var checkRequired = function (container) { 652 // This is called only for input[type="file"] 653 // If the fields is hidden, don't check for required 654 if (container.is(":hidden")) 655 return true; 656 657 var violationMessage = $('[data-sf-role="required-violation-message"]'); 658 659 var inputs = container.find('input[type="file"]'); 660 var firstInvalidInput = null; 661 for (var i = 0; i < inputs.length; i++) { 662 if (inputs[i].value) { 663 violationMessage.hide(); 664 return true; 665 } else if (!firstInvalidInput) { 666 firstInvalidInput = inputs[i]; 667 } 668 } 669 670 violationMessage.show(); 671 firstInvalidInput.focus(); 672 return false; 673 }; 674 675 var checkFileTypes = function (container, fileTypes) { 676 var hasViolations = false; 677 var inputs = container.find('input[type="file"]'); 678 for (var i = 0; i < inputs.length; i++) { 679 var violationMessage = $(inputs[i]).closest('[data-sf-role="single-file-input"]').find('[data-sf-role="filetype-violation-message"]'); 680 if (inputs[i].value) {
681 var stopIndex = inputs[i].value.lastIndexOf('.'); 682 if (stopIndex >= 0) { 683 var extension = inputs[i].value.substring(stopIndex).toLowerCase(); 684 if (fileTypes.indexOf(extension) < 0) { 685 violationMessage.show(); 686 hasViolations = true; 687 inputs[i].focus(); 688 continue; 689 } 690 } 691 } 692 693 violationMessage.hide(); 694 } 695 696 return !hasViolations; 697 }; 698 699 var checkFileSizes = function (container, min, max) { 700 if (typeof window.File == 'undefined' || typeof window.FileList == 'undefined') 701 return true; 702 703 var hasViolations = false; 704 var inputs = container.find('input[type="file"]'); 705 for (var i = 0; i < inputs.length; i++) { 706 var violationMessage = $(inputs[i]).closest('[data-sf-role="single-file-input"]').find('[data-sf-role="filesize-violation-message"]'); 707 if (inputs[i].files.length > 0) { 708 var file = inputs[i].files[0]; 709 if ((min > 0 && file.size < min) || (max > 0 && file.size > max)) { 710 violationMessage.show(); 711 hasViolations = true; 712 inputs[i].focus(); 713 continue; 714 } 715 } 716 717 violationMessage.hide(); 718 } 719 720 return !hasViolations; 721 }; 722 723 var formValidation = function (ev) { 724 var requiredValidationResult = !ev.data.config.IsRequired || checkRequired(ev.data.container); 725 var fileTypesValidationResult = ev.data.config.AcceptedFileTypes.length === 0 || checkFileTypes(ev.data.container, ev.data.config.AcceptedFileTypes); 726 var fileSizesValidationResult = !(ev.data.config.MinFileSizeInMb || ev.data.config.MaxFileSizeInMb) || checkFileSizes(ev.data.container, ev.data.config.MinFileSizeInMb * 1024 * 1024, ev.data.config.MaxFileSizeInMb * 1024 * 1024); 727 728 return requiredValidationResult && fileTypesValidationResult && fileSizesValidationResult; 729 }; 730 731 var init = function (element) { 732 var jElement = $(element); 733 var config = JSON.parse(htmlDecode(jElement.attr('data-sf-config'))); 734 735 var inputContainer = jElement.find('[data-sf-role="file-field-inputs"]'); 736 var inputTemplate = jElement.find('[data-sf-role="file-input-template"]').html(); 737 var form = jElement.closest('form'); 738 initInput(inputTemplate, inputContainer, config); 739 740 if (config.AllowMultipleFiles) { 741 $(jElement).find('[data-sf-role="add-input"]').click(function () { 742 initInput(inputTemplate, inputContainer, config); 743 }); 744 } 745 746 jElement.find('input[type="file"]').data('sfvalidator', function () { 747 return formValidation({ data: { config: config, container: inputContainer } }); 748 }); 749 750 form.submit({ config: config, container: inputContainer }, formValidation); 751 }; 752 753 $(function () { 754 var containers = $('[data-sf-role="file-field-container"]'); 755 if (!containers || containers.length < 1) 756 return; 757 758 for (var i = 0; i < containers.length; i++) { 759 init(containers[i]); 760 } 761 }); 762 }(jQuery)); 763 //email-text-field.js 764 (function ($) { 765 $(function () { 766 767 function processRules(e) { 768 if (typeof $.fn.processFormRules === 'function') { 769 $(e.target).processFormRules(); 770 } 771 } 772 773 var delayTimer; 774 function processRulesWithDelay(e) { 775 clearTimeout(delayTimer); 776 delayTimer = setTimeout(function () { 777 processRules(e); 778 }, 300); 779 } 780 781 function handleValidation(e) { 782 if (typeof e.target.validity === 'undefined') 783 return; 784 785 if (e.target.required && e.target.validity.valueMissing) {
786 var validationMessages = getValidationMessages(e.target); 787 setErrorMessage(e.target, validationMessages.required); 788 } else { 789 setErrorMessage(e.target, ''); 790 } 791 } 792 793 function onChange(e) { 794 handleValidation(e); 795 processRules(e); 796 } 797 798 function onInput(e) { 799 handleValidation(e); 800 processRulesWithDelay(e); 801 } 802 803 function invalid(e) { 804 if (typeof e.target.validity === 'undefined') 805 return; 806 807 if (_getErrorMessageContainer(e.target)) { 808 e.preventDefault(); 809 } 810 811 var validationMessages = getValidationMessages(e.target); 812 var validationRestrictions = getValidationRestrictions(e.target); 813 var isValidLength = e.target.value.length >= validationRestrictions.minLength; 814 815 if (validationRestrictions.maxLength > 0) 816 isValidLength &= e.target.value.length <= validationRestrictions.maxLength; 817 818 if (e.target.validity.valueMissing) { 819 setErrorMessage(e.target, validationMessages.required); 820 } 821 else if (e.target.validity.patternMismatch && !isValidLength) { 822 setErrorMessage(e.target, validationMessages.maxLength); 823 } 824 else if (e.target.validity.patternMismatch && isValidLength) { 825 setErrorMessage(e.target, validationMessages.regularExpression); 826 } 827 else if (!e.target.validity.valid) { 828 setErrorMessage(e.target, validationMessages.invalid); 829 } 830 } 831
832 function getValidationMessages(input) { 833 var container = $(input).parents('[data-sf-role="email-text-field-container"]'); 834 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 835 var validationMessages = JSON.parse(validationMessagesInput.val()); 836 837 return validationMessages; 838 } 839 840 function getValidationRestrictions(input) { 841 var container = $(input).parents('[data-sf-role="email-text-field-container"]'); 842 var validationRestrictionsInput = $(container).find('[data-sf-role="violation-restrictions"]'); 843 var validationRestrictions = JSON.parse(validationRestrictionsInput.val()); 844 845 return validationRestrictions; 846 } 847 848 function setErrorMessage(input, message) { 849 var errorMessagesContainer = _getErrorMessageContainer(input); 850 focusInvalidInput(input); 851 852 if (errorMessagesContainer) { 853 _toggleCustomErrorMessage(errorMessagesContainer, message); 854 } else { 855 input.setCustomValidity(message); 856 } 857 } 858 859 function _toggleCustomErrorMessage(container, message) { 860 container.innerText = message; 861 862 if (message === '') { 863 container.style.display = 'none'; 864 } else { 865 container.style.display = 'block'; 866 } 867 } 868 869 function _getErrorMessageContainer(input) { 870 var container = $(input).closest('[data-sf-role="email-text-field-container"]')[0]; 871 if (container) { 872 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 873 return errorMessagesContainer; 874 } 875 876 return null; 877 } 878 879 function init() { 880 var containers = $('[data-sf-role="email-text-field-container"]'); 881 882 if (!containers || containers.length < 1) 883 return; 884 885 for (var i = 0; i < containers.length; i++) { 886 var input = $(containers[i]).find('[data-sf-role="email-text-field-input"]'); 887 if (input) { 888 input.on('change', onChange); 889 input.on('input', onInput); 890 input.on('invalid', invalid); 891 } 892 } 893 } 894 init(); 895 }); 896 }(jQuery)); 897 //multiple-choice-field.js 898 (function ($) { 899 var changeOrInput = function (e) { 900 var container = $(e.target).parents('[data-sf-role="multiple-choice-field-container"]'); 901 var inputs = $(container).find('[data-sf-role="multiple-choice-field-input"]'); 902 inputs.each(function (index, input) { 903 input.validity.valueMissing = false; 904 setErrorMessage(input, ''); 905 }); 906 }; 907 908 var invalid = function (e) { 909 var validationMessages = getValidationMessages(e.target); 910 911 if (_getErrorMessageContainer(e.target)) { 912 e.preventDefault(); 913 } 914 915 if (e.target.validity.valueMissing) { 916 setErrorMessage(e.target, validationMessages.required); 917 } 918 919 var isValidLength = e.target.value.length <= 255; 920 if (e.target.validity.patternMismatch && !isValidLength) { 921 setErrorMessage(e.target, validationMessages.maxLength); 922 } 923 }; 924 925 var getValidationMessages = function (input) { 926 var container = $(input).parents('[data-sf-role="multiple-choice-field-container"]'); 927 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 928 var validationMessages = JSON.parse(validationMessagesInput.val()); 929 930 return validationMessages; 931 }; 932 933 var setErrorMessage = function (input, message) { 934 var errorMessagesContainer = _getErrorMessageContainer(input); 935 focusInvalidInput(input); 936 937 if (errorMessagesContainer) { 938 _toggleCustomErrorMessage(errorMessagesContainer, message); 939 } else { 940 input.setCustomValidity(message); 941 } 942 }; 943 944 var _toggleCustomErrorMessage = function (container, message) { 945 container.innerText = message; 946 947 if (message === '') { 948 container.style.display = 'none'; 949 } else { 950 container.style.display = 'block'; 951 } 952 }; 953 954 var _getErrorMessageContainer = function (input) { 955 var container = $(input).closest('[data-sf-role="multiple-choice-field-container"]')[0]; 956 if (container) { 957 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 958 return errorMessagesContainer; 959 } 960 961 return null; 962 }; 963 964 $(function () { 965 var containers = $('[data-sf-role="multiple-choice-field-container"]'); 966 967 if (!containers || containers.length < 1) 968 return; 969 970 var attachHandlers = function (input) { 971 input.on('change', function (e) { 972 changeOrInput(e); 973 if (typeof $.fn.processFormRules == 'function') 974 $(e.target).processFormRules();
975 }); 976 input.on('input', changeOrInput); 977 input.on('invalid', invalid); 978 }; 979 980 var radioClickHandler = function (e) { 981 var container = $(e.target).parents('[data-sf-role="multiple-choice-field-container"]'); 982 var radios = container.find('input[data-sf-role="multiple-choice-field-input"]'); 983 var otherInput = $(container.find('[data-sf-multiple-choice-role="other-choice-text"]').first()); 984 var otherRadio = $(container.find('[data-sf-multiple-choice-role="other-choice-radio"]').first()); 985 var otherRadioIndex = radios.index(otherRadio); 986 var currentIndex = radios.index($(e.target)); 987 var isRequired = $(radios).first().attr('required'); 988 989 if (currentIndex == otherRadioIndex) { 990 otherInput.attr('type', 'text'); 991 992 if (isRequired) 993 otherInput.attr('required', 'required'); 994 995 otherInput.attr('pattern', '.{0,255}'); 996 otherInput.on('invalid', invalid); 997 } 998 else { 999 otherInput.attr('type', 'hidden'); 1000 otherInput.removeAttr('required'); 1001 } 1002 }; 1003 1004 var inputChangeHandler = function (e) { 1005 var container = $(e.target).parents('[data-sf-role="multiple-choice-field-container"]'); 1006 var otherRadio = $(container.find('[data-sf-multiple-choice-role="other-choice-radio"]').first()); 1007 otherRadio.val($(e.target).val()); 1008 }; 1009 1010 for (var i = 0; i < containers.length; i++) { 1011 var container = $(containers[i]); 1012 var inputs = container.find('[data-sf-role="multiple-choice-field-input"]'); 1013 1014 attachHandlers(inputs); 1015 1016 var radios = container.find('input[data-sf-role="multiple-choice-field-input"]'); 1017 1018 var otherInput = $(container.find('[data-sf-multiple-choice-role="other-choice-text"]').first()); 1019 1020 radios.click(radioClickHandler); 1021 otherInput.change(inputChangeHandler); 1022 } 1023 }); 1024 }(jQuery)); 1025 //navigation-field.js 1026 ; (function ($) { 1027 if (typeof window.FormData === 'undefined') 1028 return; 1029 1030 $(function () { 1031 var formContainers = $('[data-sf-role="form-container"]'); 1032 1033 var initializeFormContainer = function (element) { 1034 var formElement = $(element); 1035 var formStepsContainers = formElement.find('[data-sf-role="separator"]'); 1036 var navigationFieldContainers = formElement.find('[data-sf-role="navigation-field-container"]'); 1037 var srProgressbar = formElement.find('[data-sf-role="sr-progressbar"]'); 1038 1039 // Selects element with class sf-sr-only and adds text: Step # of # 1040 var modifySrOnlyData = function (currentPage, totalPages, pageTitle) { 1041 // TODO: Accessibility is not implemented no logic is required 1042 if (srProgressbar.length <= 0) { 1043 return; 1044 } 1045 1046 var stepOfResource = $(element).find('[data-sf-role="step-of-resources"]').val(); 1047 var valueText = stepOfResource.replace("{0}", currentPage).replace("{1}", totalPages) + ": " + pageTitle; 1048 srProgressbar.attr("aria-valuenow", currentPage); 1049 srProgressbar.attr("aria-valueText", valueText); 1050 }; 1051 1052 var updateNavigationFields = function (navigationElements, index) { 1053 1054 navigationElements.each(function (navIndex, navigationElement) { 1055 var navElement = $(navigationElement); 1056 var pages = $(navigationElement).find('[data-sf-navigation-index]'); 1057 var numberOfAllSteps = formStepsContainers.length; 1058 var progressInPercent = Math.round((index / numberOfAllSteps) * 100); 1059 1060 var progressBar = formElement.find('[data-sf-role="progress-bar"]'); 1061 var progressPercent = formElement.find('[data-sf-role="progress-percent"]'); 1062 1063 if (progressBar && progressBar.length > 0) { 1064 progressBar.width(progressInPercent + '%'); 1065 } 1066 1067 if (progressPercent && progressPercent.length > 0) { 1068 progressPercent.text(progressInPercent + '%'); 1069 } 1070 1071 if (pages && pages.length > 0) { 1072 pages.each(function (i, page) { 1073 var pageIndex = parseInt($(page).data("sfNavigationIndex")); 1074 var pageTitleWrp = $(page).find('[data-sf-page-title]'); 1075 var pageTitle = ""; 1076 1077 var activeCssClass = $(navElement).attr("data-sf-active-css-class") || "active"; 1078 var pastCssClass = $(navElement).attr("data-sf-past-css-class") || "past"; 1079 var futureCssClass = $(navElement).attr("data-sf-future-css-class") || "future"; 1080 1081 var pastIndicatorPast = $(page).find("[data-sf-progress-indicator='past']"); 1082 var pastIndicatorIncomplete = $(page).find("[data-sf-progress-indicator='incomplete']"); 1083 1084 // TODO: Accessibility is implemented 1085 if (pageTitleWrp.length > 0) { 1086 pageTitle = $(pageTitleWrp).data("sfPageTitle"); 1087 } 1088 $(page).removeClass(activeCssClass).removeClass(futureCssClass).removeClass(pastCssClass); 1089 1090 if (pageIndex !== index) { 1091 if (pageIndex < index) { 1092 $(page).addClass(pastCssClass); 1093 $(pastIndicatorPast).show(); 1094 $(pastIndicatorIncomplete).hide(); 1095 } else if (pageIndex > index) { 1096 $(page).addClass(futureCssClass); 1097 $(pastIndicatorPast).hide(); 1098 $(pastIndicatorIncomplete).show(); 1099 } 1100 } else { 1101 $(page).addClass(activeCssClass); 1102 $(pastIndicatorPast).hide(); 1103 $(pastIndicatorIncomplete).show(); 1104 1105 // Because pageIndex starts from 0 we increase it by 1 so it is simple to read 1106 var currentPage = ++pageIndex; 1107 modifySrOnlyData(currentPage, pages.length, pageTitle); 1108 } 1109 }); 1110 } 1111 1112 }); 1113 }; 1114 1115 // Initialize navigation fields 1116 updateNavigationFields(navigationFieldContainers, 0); 1117 1118 formElement.on("form-page-changed", function (e, index, previousIndex) { 1119 updateNavigationFields(navigationFieldContainers, index); 1120 }); 1121 }; 1122 1123 1124 formContainers.each(function (i, element) { 1125 initializeFormContainer(element); 1126 }); 1127 1128 // This implementation is only for the Form preview mode 1129 var isPreviewMode = window.location.href.indexOf("/Preview") !== -1; 1130 if (formContainers.length === 0 && isPreviewMode) { 1131 var separator = $('[data-sf-role="separator"]'); 1132 if (separator.length > 0) { 1133 initializeFormContainer(separator.parent()); 1134 } 1135 } 1136 }); 1137 })(jQuery); 1138 //page-break.js 1139 ; (function ($) { 1140 if (typeof window.FormData === 'undefined') 1141 return; 1142 1143 $(function () { 1144 var selectors = { 1145 separator: '[data-sf-role="separator"]', 1146 formContainer: '[data-sf-role="form-container"]', 1147 previousButton: '[data-sf-btn-role="prev"]', 1148 nextButton: '[data-sf-btn-role="next"]' 1149 }; 1150 var formContainers = $(selectors.formContainer); 1151 1152 var initializeFormContainer = function (element) { 1153 var formElement = $(element); 1154 var formStepsContainers = formElement.find(selectors.separator); 1155 var formStepIndex = 0; 1156 var skipToPageCollection = []; 1157 var submitButton = null; 1158 var isSubmitButtonAdded = false; 1159 var stepNewForm = null; 1160 1161 formElement.on("form-page-changed", function (e, index, previousIndex) { 1162 formStepIndex = index; 1163 }); 1164 1165 formElement.on("form-page-skip", function (e, skipToPageList) { 1166 skipToPageCollection = skipToPageList; 1167 }); 1168 1169 formStepsContainers.each(function (i, element) { 1170 $(element).hide(); 1171 }); 1172 1173 formStepsContainers.first().show(); 1174 formStepsContainers.first().find(selectors.previousButton).hide(); 1175 formStepsContainers.last().find(selectors.nextButton).hide(); 1176 1177 var tryGoToNextStep = function (currentStepContainer, continueFunction) { 1178 var formContainer = $(currentStepContainer); 1179 stepNewForm = $('form#stepNewForm'); 1180 if (stepNewForm.length === 0) { 1181 formContainer.wrap('<form id="stepNewForm"></form>'); 1182 stepNewForm = formContainer.parent(); 1183 } 1184 1185 stepNewForm.one('submit', function (e) { 1186 e.preventDefault(); 1187 if (e.target.innerHTML.length > 0) { 1188 var inputs = stepNewForm.find('input'); 1189 var isValid = true; 1190 for (var i = 0; i < inputs.length; i++) { 1191 var jInput = $(inputs[i]); 1192 if (typeof (jInput.data('sfvalidator')) === 'function') 1193 isValid = jInput.data('sfvalidator')() && isValid; 1194 } 1195 1196 if (!isValid) { 1197 return false; 1198 } 1199 1200 formContainer.unwrap(); 1201 1202 if (isSubmitButtonAdded) { 1203 submitButton.remove(); 1204 } 1205 1206 continueFunction(); 1207 } 1208 }); 1209 1210 submitButton = formContainer.find('input#stepNewFormSubmit'); 1211 1212 if (submitButton.length === 0) { 1213 // If we do not have submit button in this step we need to add a hidden submit button in order to click it and trigger the native HTML 5 1214 // browser validation 1215 submitButton = $('<input id="stepNewFormSubmit" style="display:none;" type="submit"/>'); 1216 formContainer.append(submitButton); 1217 isSubmitButtonAdded = true; 1218 } 1219 1220 submitButton.click(); 1221 }; 1222 1223 var getSkipToPageItem = function (pageIndex) { 1224 var item = null; 1225 for (var i = 0;
1225 i < skipToPageCollection.length; i++) { 1226 if (skipToPageCollection[i].SkipFromPage === pageIndex) return skipToPageCollection[i]; 1227 } 1228 1229 return item; 1230 }; 1231 1232 var getSkipFromPageItem = function (pageIndex) { 1233 var item = null; 1234 for (var i = 0; i < skipToPageCollection.length; i++) { 1235 if (skipToPageCollection[i].SkipToPage === pageIndex) return skipToPageCollection[i]; 1236 } 1237 1238 return item; 1239 }; 1240 1241 var focusForm = function () { 1242 var form = $(formElement).find("form"); 1243 1244 // TODO: Accessibility is not implemented no logic is required 1245 if (form.length <= 0) { 1246 return; 1247 } 1248 1249 form.attr("tabindex", 0); 1250 form.focus(); 1251 form.removeAttr("tabindex"); 1252 }; 1253 1254 var separatorsNext = formStepsContainers.find(selectors.nextButton); 1255 1256 separatorsNext.click(function (e) { 1257 e.preventDefault(); 1258 1259 var currentStepContainer = $(e.target).closest(selectors.separator); 1260 tryGoToNextStep(currentStepContainer, function () { 1261 var previousIndex = formStepIndex; 1262 currentStepContainer.hide(); 1263 1264 if (skipToPageCollection && skipToPageCollection.length > 0) { 1265 var skipItem = getSkipToPageItem(formStepIndex); 1266 if (skipItem) { 1267 formStepIndex = skipItem.SkipToPage; 1268 } 1269 else { 1270 formStepIndex++; 1271 } 1272 } 1273 else { 1274 formStepIndex++; 1275 } 1276 1277 $(formStepsContainers[formStepIndex]).show(); 1278 formElement.trigger("form-page-changed", [formStepIndex, previousIndex]); 1279 focusForm(); 1280 }); 1281 }); 1282 1283 var separatorsPrev = formStepsContainers.find(selectors.previousButton); 1284 separatorsPrev.click(function (e) { 1285 e.preventDefault(); 1286 1287 var previousIndex = formStepIndex; 1288 var stepNewForm = $('form#stepNewForm'); 1289 var currentContainer = $(e.target).closest(selectors.separator); 1290 if (stepNewForm.children(selectors.separator).length > 0) { 1291 currentContainer.unwrap(); 1292 } 1293 1294 currentContainer.hide(); 1295 1296 if (skipToPageCollection && skipToPageCollection.length > 0) { 1297 var skipItem = getSkipFromPageItem(formStepIndex); 1298 if (skipItem) { 1299 formStepIndex = skipItem.SkipFromPage; 1300 } 1301 else { 1302 formStepIndex--; 1303 } 1304 } 1305 else { 1306 formStepIndex--; 1307 } 1308 1309 $(formStepsContainers[formStepIndex]).show(); 1310 formElement.trigger("form-page-changed", [formStepIndex, previousIndex]); 1311 focusForm(); 1312 }); 1313 }; 1314 1315 formContainers.each(function (i, element) { 1316 initializeFormContainer(element); 1317 }); 1318 1319 // This implementation is only for the Form preview mode 1320 var isPreviewMode = window.location.href.indexOf("/Preview") !== -1; 1321 if (formContainers.length === 0 && isPreviewMode) { 1322 var separator = $(selectors.separator); 1323 if (separator.length > 0) { 1324 initializeFormContainer(separator.parent()); 1325 } 1326 } 1327 }); 1328 })(jQuery); 1329 //paragraph-text-field.js 1330 (function ($) { 1331 $(function () { 1332 1333 function processRules(e) { 1334 if (typeof $.fn.processFormRules === 'function') { 1335 $(e.target).processFormRules();
1336 } 1337 } 1338 1339 var delayTimer; 1340 function processRulesWithDelay(e) { 1341 clearTimeout(delayTimer); 1342 delayTimer = setTimeout(function () { 1343 processRules(e); 1344 }, 300); 1345 } 1346 1347 function handleValidation(e) { 1348 if (typeof e.target.validity === 'undefined') 1349 return; 1350 1351 var validationMessages = getValidationMessages(e.target); 1352 1353 if (e.target.required && e.target.validity.valueMissing) { 1354 setErrorMessage(e.target, validationMessages.required); 1355 } 1356 else if (e.target.validity.tooShort || e.target.validity.tooLong) { 1357 setErrorMessage(e.target, validationMessages.maxLength); 1358 } 1359 else { 1360 setErrorMessage(e.target, ''); 1361 } 1362 } 1363 1364 function onChange(e) { 1365 handleValidation(e); 1366 processRules(e); 1367 } 1368 1369 function onInput(e) { 1370 handleValidation(e); 1371 processRulesWithDelay(e); 1372 } 1373
1374 function invalid(e) { 1375 if (typeof e.target.validity === 'undefined') 1376 return; 1377 1378 if (_getErrorMessageContainer(e.target)) { 1379 e.preventDefault(); 1380 } 1381 1382 var validationMessages = getValidationMessages(e.target); 1383 1384 if (e.target.validity.valueMissing) { 1385 setErrorMessage(e.target, validationMessages.required); 1386 } 1387 else if (e.target.validity.tooShort || e.target.validity.tooLong) { 1388 setErrorMessage(e.target, validationMessages.maxLength); 1389 } 1390 else if (e.target.validity.patternMismatch) { 1391 setErrorMessage(e.target, validationMessages.maxLength); 1392 } 1393 } 1394 1395 function getValidationMessages(input) { 1396 var container = $(input).parents('[data-sf-role="paragraph-text-field-container"]'); 1397 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 1398 var validationMessages = JSON.parse(validationMessagesInput.val()); 1399 1400 return validationMessages; 1401 } 1402 1403 function setErrorMessage(input, message) { 1404 var errorMessagesContainer = _getErrorMessageContainer(input); 1405 focusInvalidInput(input); 1406 1407 if (errorMessagesContainer) { 1408 _toggleCustomErrorMessage(errorMessagesContainer, message); 1409 } else { 1410 input.setCustomValidity(message); 1411 } 1412 } 1413 1414 function _toggleCustomErrorMessage(container, message) { 1415 container.innerText = message; 1416 1417 if (message === '') { 1418 container.style.display = 'none'; 1419 } else { 1420 container.style.display = 'block'; 1421 } 1422 } 1423 1424 function _getErrorMessageContainer(input) { 1425 var container = $(input).closest('[data-sf-role="paragraph-text-field-container"]')[0]; 1426 if (container) { 1427 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 1428 return errorMessagesContainer; 1429 } 1430 1431 return null; 1432 } 1433 1434 function init() { 1435 var containers = $('[data-sf-role="paragraph-text-field-container"]'); 1436 1437 if (!containers || containers.length < 1) 1438 return; 1439 1440 for (var i = 0; i < containers.length; i++) { 1441 var textarea = $(containers[i]).find('[data-sf-role="paragraph-text-field-textarea"]'); 1442 1443 if (textarea) { 1444 textarea.on('change', onChange); 1445 textarea.on('input', onInput); 1446 textarea.on('invalid', invalid); 1447 } 1448 } 1449 } 1450 1451 init(); 1452 }); 1453 }(jQuery)); 1454 //submit-button.js 1455 (function ($) { 1456 $(function () { 1457 $('button[type="submit"]').each(function () { 1458 $(this).closest('form').submit(function (ev) { 1459 if (!ev.isDefaultPrevented()) { 1460 var button = $(this).find('button[type="submit"]'); 1461 1462 // If the submit button is not visible and there is a page break button next that is visible 1463 // we asume that we are in case with multi step form, where the enter button is hit and the form is submited, 1464 // so that we need to click next instead of submitting the form. 1465 var isSubmitButtonVisible = $(button).is(":visible"); 1466 var firstPageBreak = $(ev.target).find('[data-sf-btn-role="next"]:visible')[0]; 1467 if (!isSubmitButtonVisible && firstPageBreak) { 1468 firstPageBreak.click(); 1469 ev.preventDefault(); 1470 1471 return; 1472 } 1473 1474 button.prop('disabled', true); 1475 1476 //we need first to validate all inputs which require server side validation (cannot be validated client-side) 1477 //before the form is submitted and submit the form only if all are valid - e.g. captcha is such control 1478 var allInputs = ev.target.querySelectorAll('input, textarea', 'select'); 1479 var widgetValidators = []; 1480 1481 for (var i = 0; i < allInputs.length; i++) { 1482 if ($(allInputs[i]).data('widget-validator')) { 1483 widgetValidators.push($(allInputs[i]).data('widget-validator')); 1484 } 1485 } 1486 1487 if (widgetValidators.length) { 1488 ev.preventDefault(); 1489 var deferreds = []; 1490 for (i = 0; i < widgetValidators.length; i++) { 1491 deferreds.push(widgetValidators[i]()); 1492 } 1493 1494 $.when.apply($, deferreds).done(function () { 1495 for (var i = 0; i < widgetValidators.length; i++) { 1496 if (!arguments[i]) { 1497 return false; 1498 } 1499 } 1500 1501 ev.target.submit(); 1502 }).always(function () { 1503 button.prop('disabled', false); 1504 }); 1505 } 1506 } 1507 }); 1508 }); 1509 // Reset the counter back to 1 every time submit button is clicked 1510 $('button[type="submit"]').on('click', function() { 1511 counter = 1; 1512 }); 1513 }); 1514 }(jQuery)); 1515 //text-field.js 1516 (function ($) { 1517 $(function () { 1518 1519 function processRules(e) { 1520 if (typeof $.fn.processFormRules === 'function') { 1521 $(e.target).processFormRules();
1522 } 1523 } 1524 1525 var delayTimer; 1526 function processRulesWithDelay(e) { 1527 clearTimeout(delayTimer); 1528 delayTimer = setTimeout(function () { 1529 processRules(e); 1530 }, 300); 1531 } 1532 1533 function handleValidation(e) { 1534 if (typeof e.target.validity === 'undefined') 1535 return; 1536 1537 if (e.target.required && e.target.validity.valueMissing) { 1538 var validationMessages = getValidationMessages(e.target); 1539 setErrorMessage(e.target, validationMessages.required); 1540 } else { 1541 setErrorMessage(e.target, ''); 1542 } 1543 } 1544 1545 function onChange(e) { 1546 handleValidation(e); 1547 processRules(e); 1548 } 1549 1550 function onInput(e) { 1551 handleValidation(e); 1552 processRulesWithDelay(e); 1553 } 1554
1555 function invalid(e) { 1556 if (typeof e.target.validity === 'undefined') 1557 return; 1558 1559 if (_getErrorMessageContainer(e.target)) { 1560 e.preventDefault(); 1561 } 1562 1563 var validationMessages = getValidationMessages(e.target); 1564 var validationRestrictions = getValidationRestrictions(e.target); 1565 var isValidLength = e.target.value.length >= validationRestrictions.minLength; 1566 1567 if (validationRestrictions.maxLength > 0) 1568 isValidLength &= e.target.value.length <= validationRestrictions.maxLength; 1569 1570 if (e.target.validity.valueMissing) { 1571 setErrorMessage(e.target, validationMessages.required); 1572 } 1573 else if (e.target.validity.patternMismatch && !isValidLength) { 1574 setErrorMessage(e.target, validationMessages.maxLength); 1575 } 1576 else if (e.target.validity.patternMismatch && isValidLength) { 1577 setErrorMessage(e.target, validationMessages.regularExpression); 1578 } 1579 else if (!e.target.validity.valid) { 1580 setErrorMessage(e.target, validationMessages.invalid); 1581 } 1582 } 1583
1584 function getValidationMessages(input) { 1585 var container = $(input).parents('[data-sf-role="text-field-container"]'); 1586 var validationMessagesInput = $(container).find('[data-sf-role="violation-messages"]'); 1587 var validationMessages = JSON.parse(validationMessagesInput.val()); 1588 1589 return validationMessages; 1590 } 1591 1592 function getValidationRestrictions(input) { 1593 var container = $(input).parents('[data-sf-role="text-field-container"]'); 1594 var validationRestrictionsInput = $(container).find('[data-sf-role="violation-restrictions"]'); 1595 var validationRestrictions = JSON.parse(validationRestrictionsInput.val()); 1596 1597 return validationRestrictions; 1598 } 1599 1600 function setErrorMessage(input, message) { 1601 var errorMessagesContainer = _getErrorMessageContainer(input); 1602 focusInvalidInput(input); 1603 1604 if (errorMessagesContainer) { 1605 _toggleCustomErrorMessage(errorMessagesContainer, message); 1606 } else { 1607 input.setCustomValidity(message); 1608 } 1609 } 1610 1611 function _toggleCustomErrorMessage(container, message) { 1612 container.innerText = message; 1613 1614 if (message === '') { 1615 container.style.display = 'none'; 1616 } else { 1617 container.style.display = 'block'; 1618 } 1619 } 1620 1621 function _getErrorMessageContainer(input) { 1622 var container = $(input).closest('[data-sf-role="text-field-container"]')[0]; 1623 if (container) { 1624 var errorMessagesContainer = container.querySelector('[data-sf-role="error-message"]'); 1625 return errorMessagesContainer; 1626 } 1627 1628 return null; 1629 } 1630 1631 function init() { 1632 var containers = $('[data-sf-role="text-field-container"]'); 1633 1634 if (!containers || containers.length < 1) 1635 return; 1636 1637 for (var i = 0; i < containers.length; i++) { 1638 var input = $(containers[i]).find('[data-sf-role="text-field-input"]'); 1639 1640 if (input) { 1641 input.on('change', onChange); 1642 input.on('input', onInput); 1643 input.on('invalid', invalid); 1644 } 1645 } 1646 } 1647 1648 init(); 1649 }); 1650 }(jQuery)); 1651})();
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.