1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 | 1× 1× 1× 1× 1× 1× 1× 19× 19× 19× 1× 279× 279× 279× 279× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 84× 84× 84× 84× 84× 84× 84× 84× 84× 84× 84× 34× 84× 84× 1× 84× 84× 84× 82× 82× 82× 82× 2× 82× 1× 1× 100× 100× 6× 94× 1× 5× 2× 3× 1× 2× 1× 1× 1× 1× 275× 275× 259× 259× 307× 16× 16× 34× 34× 16× 1× 70× 70× 1× 1068× 1068× 1× 67× 67× 67× 67× 67× 74× 74× 67× 67× 67× 1× 33× 33× 33× 1× 2× 36× 36× 36× 36× 1× 150× 1× 1× 1× 1× 1× 1× 1× 1× 301× 301× 301× 301× 690× 690× 690× 690× 690× 690× 690× 690× 690× 686× 686× 686× 690× 690× 690× 1× 82× 82× 82× 236× 236× 236× 5664× 5664× 295× 15× 15× 12× 12× 12× 207× 18× 207× 6× 6× 3× 3× 3× 52× 33× 19× 236× 96× 1× 33× 33× 33× 33× 33× 15× 9× 9× 3× 3× 3× 3× 18× 18× 3× 3× 3× 3× 3× 3× 3× 3× 6× 6× 6× 3× 3× 1× 106× 106× 106× 106× 5× 5× 106× 7× 99× 15× 84× 5× 106× 1× 83× 238× 238× 24× 214× 8× 206× 206× 83× 83× 1× 68× 208× 208× 68× 68× 1× 5× 5× 5× 4× 2× 2× 1× 1× 3× 3× 3× 3× 1× 2× 1× 1× 4× 4× 4× 2× 2× 1× 1× 2× 2× 2× 1× 3× 3× 2× 1× 1× 231× 1× 293× 1× 292× 292× 292× 292× 292× 292× 292× 2× 292× 2× 292× 291× 2× 291× 1× 291× 291× 303× 303× 303× 303× 303× 180× 180× 180× 180× 180× 180× 180× 180× 180× 180× 180× 84× 96× 180× 180× 180× 180× 180× 180× 180× 180× 180× 50× 180× 123× 123× 123× 1× 1× 180× 17× 17× 17× 17× 1× 303× 303× 303× 303× 303× 303× 303× 303× 2× 2× 301× 14× 1× 13× 287× 1× 308× 308× 308× 87× 112× 112× 308× 1× 84× 84× 1× 1× 1× 84× 84× 1× 83× 1× 1× 1× 82× 3× 79× 79× 79× 79× 79× 84× 84× 84× 84× 1× 1061× 1061× 532× 1061× 1× 570× 289× 289× 115× 1× 96× 96× 96× 1× 843× 390× 390× 390× 390× 390× 390× 390× 390× 390× 1× 180× 17× 17× 17× 1× 539× 539× 1× 1× 58× 45× 18× 7× 5× 2× 21× 10× 14× 17× 12× 12× 18× 18× 16× 3× 3× 3× 3× 13× 13× 10× 3× 1× 1× 1× 1× 1× 2× 18× 12× 6× 3× 3× 3× 2× 2× 2× 2× 2× 1× 9× 4× 4× 4× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /* istanbul ignore next */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /* istanbul ignore next */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1, ej2_base_2, ej2_base_3, ej2_base_4) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * global declarations */ exports.regex = { /* eslint-disable no-useless-escape */ EMAIL: new RegExp('^[A-Za-z0-9._%+-]{1,}@[A-Za-z0-9._%+-]{1,}([.]{1}[a-zA-Z0-9]{2,}' + '|[.]{1}[a-zA-Z0-9]{2,4}[.]{1}[a-zA-Z0-9]{2,4})$'), /* eslint-disable-next-line security/detect-unsafe-regex */ URL: /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/m, DATE_ISO: new RegExp('^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'), DIGITS: new RegExp('^[0-9]*$'), PHONE: new RegExp('^[+]?[0-9]{9,13}$'), CREDITCARD: new RegExp('^\\d{13,16}$') /* eslint-enable no-useless-escape */ }; /** * ErrorOption values * * @private */ var ErrorOption; (function (ErrorOption) { /** * Defines the error message. */ ErrorOption[ErrorOption["Message"] = 0] = "Message"; /** * Defines the error element type. */ ErrorOption[ErrorOption["Label"] = 1] = "Label"; })(ErrorOption = exports.ErrorOption || (exports.ErrorOption = {})); /** * FormValidator class enables you to validate the form fields based on your defined rules * ```html * <form id='formId'> * <input type='text' name='Name' /> * <input type='text' name='Age' /> * </form> * <script> * let formObject = new FormValidator('#formId', { * rules: { Name: { required: true }, Age: { range: [18, 30] } }; * }); * formObject.validate(); * </script> * ``` */ var FormValidator = /** @class */ (function (_super) { __extends(FormValidator, _super); /** * Initializes the FormValidator. * * @param {string | HTMLFormElement} element - Specifies the element to render as component. * @param {FormValidatorModel} options - Specifies the FormValidator model. * @private */ function FormValidator(element, options) { var _this = _super.call(this, options, element) || this; _this.validated = []; _this.errorRules = []; _this.allowSubmit = false; _this.required = 'required'; _this.infoElement = null; _this.inputElement = null; _this.selectQuery = 'input:not([type=reset]):not([type=button]), select, textarea'; // eslint-disable-next-line @typescript-eslint/no-explicit-any _this.localyMessage = {}; /** * Specifies the default messages for validation rules. * * @default { List of validation message } */ _this.defaultMessages = { required: 'This field is required.', email: 'Please enter a valid email address.', url: 'Please enter a valid URL.', date: 'Please enter a valid date.', dateIso: 'Please enter a valid date ( ISO ).', creditcard: 'Please enter valid card number', number: 'Please enter a valid number.', digits: 'Please enter only digits.', maxLength: 'Please enter no more than {0} characters.', minLength: 'Please enter at least {0} characters.', rangeLength: 'Please enter a value between {0} and {1} characters long.', range: 'Please enter a value between {0} and {1}.', max: 'Please enter a value less than or equal to {0}.', min: 'Please enter a value greater than or equal to {0}.', regex: 'Please enter a correct value.', tel: 'Please enter a valid phone number.', pattern: 'Please enter a correct pattern value.', equalTo: 'Please enter the valid match text' }; if (typeof _this.rules === 'undefined') { _this.rules = {}; } _this.l10n = new ej2_base_4.L10n('formValidator', _this.defaultMessages, _this.locale); if (_this.locale) { _this.localeFunc(); } ej2_base_3.onIntlChange.on('notifyExternalChange', _this.afterLocalization, _this); element = typeof element === 'string' ? ej2_base_1.select(element, document) : element; // Set novalidate to prevent default HTML5 form validation if (_this.element != null) { _this.element.setAttribute('novalidate', ''); _this.inputElements = ej2_base_1.selectAll(_this.selectQuery, _this.element); _this.createHTML5Rules(); _this.wireEvents(); } else { return undefined; } return _this; } FormValidator_1 = FormValidator; /* eslint-enable @typescript-eslint/no-explicit-any */ /** * Add validation rules to the corresponding input element based on `name` attribute. * * @param {string} name `name` of form field. * @param {Object} rules Validation rules for the corresponding element. * @returns {void} */ FormValidator.prototype.addRules = function (name, rules) { Eif (name) { // eslint-disable-next-line no-prototype-builtins if (this.rules.hasOwnProperty(name)) { ej2_base_2.extend(this.rules["" + name], rules, {}); } else { this.rules["" + name] = rules; } } }; /** * Remove validation to the corresponding field based on name attribute. * When no parameter is passed, remove all the validations in the form. * * @param {string} name Input name attribute value. * @param {string[]} rules List of validation rules need to be remove from the corresponding element. * @returns {void} */ FormValidator.prototype.removeRules = function (name, rules) { if (!name && !rules) { this.rules = {}; } else if (this.rules["" + name] && !rules) { delete this.rules["" + name]; } else if (!ej2_base_2.isNullOrUndefined(this.rules["" + name] && rules)) { for (var i = 0; i < rules.length; i++) { delete this.rules["" + name][rules[parseInt(i.toString(), 10)]]; } } else { return; } }; /** * Validate the current form values using defined rules. * Returns `true` when the form is valid otherwise `false` * * @param {string} selected - Optional parameter to validate specified element. * @returns {boolean} - Returns form validation status. */ FormValidator.prototype.validate = function (selected) { var rules = Object.keys(this.rules); if (selected && rules.length) { this.validateRules(selected); //filter the selected element it don't have any valid input element return rules.indexOf(selected) !== -1 && this.errorRules.filter(function (data) { return data.name === selected; }).length === 0; } else { this.errorRules = []; for (var _i = 0, rules_1 = rules; _i < rules_1.length; _i++) { var name_1 = rules_1[_i]; this.validateRules(name_1); } return this.errorRules.length === 0; } }; /** * Reset the value of all the fields in form. * * @returns {void} */ FormValidator.prototype.reset = function () { this.element.reset(); this.clearForm(); }; /** * Get input element by name. * * @param {string} name - Input element name attribute value. * @returns {HTMLInputElement} - Returns the input element. */ FormValidator.prototype.getInputElement = function (name) { this.inputElement = (ej2_base_1.select('[name="' + name + '"]', this.element)); return this.inputElement; }; /** * Destroy the form validator object and error elements. * * @returns {void} */ FormValidator.prototype.destroy = function () { this.reset(); this.unwireEvents(); this.rules = {}; var elements = ej2_base_1.selectAll('.' + this.errorClass + ', .' + this.validClass, this.element); for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) { var element = elements_1[_i]; ej2_base_1.detach(element); } _super.prototype.destroy.call(this); this.infoElement = null; ej2_base_3.onIntlChange.off('notifyExternalChange', this.afterLocalization); }; /** * @param {FormValidatorModel} newProp - Returns the dynamic property value of the component. * @param {FormValidatorModel} oldProp - Returns the previous property value of the component. * @returns {void} * @private */ // eslint-disable-next-line @typescript-eslint/no-unused-vars FormValidator.prototype.onPropertyChanged = function (newProp, oldProp) { for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) { var prop = _a[_i]; switch (prop) { case 'locale': this.localeFunc(); break; } } }; /** * @private * @returns {void} */ FormValidator.prototype.localeFunc = function () { for (var _i = 0, _a = Object.keys(this.defaultMessages); _i < _a.length; _i++) { var key = _a[_i]; this.l10n.setLocale(this.locale); var value = this.l10n.getConstant(key); this.localyMessage["" + key] = value; } }; /** * @private * @returns {string} - Returns the component name. */ FormValidator.prototype.getModuleName = function () { return 'formvalidator'; }; /** * @param {any} args - Specifies the culture name. * @returns {void} * @private */ FormValidator.prototype.afterLocalization = function (args) { this.locale = args.locale; this.localeFunc(); }; /** * Allows you to refresh the form validator base events to the elements inside the form. * * @returns {void} */ FormValidator.prototype.refresh = function () { this.unwireEvents(); this.inputElements = ej2_base_1.selectAll(this.selectQuery, this.element); this.wireEvents(); }; FormValidator.prototype.clearForm = function () { this.errorRules = []; this.validated = []; var elements = ej2_base_1.selectAll(this.selectQuery, this.element); for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) { var element = elements_2[_i]; var input = element; input.removeAttribute('aria-invalid'); var inputParent = input.parentElement; var grandParent = inputParent.parentElement; Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper') || (input.classList.contains('e-input') && inputParent.classList.contains('e-input-group'))) { inputParent.classList.remove(this.errorClass); } else Iif ((grandParent != null) && (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper'))) { grandParent.classList.remove(this.errorClass); } else { input.classList.remove(this.errorClass); } if (input.name.length > 0) { this.getInputElement(input.name); this.getErrorElement(input.name); this.hideMessage(input.name); } Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper') || (input.classList.contains('e-input') && inputParent.classList.contains('e-input-group'))) { inputParent.classList.remove(this.validClass); } else Iif ((grandParent != null) && (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper'))) { grandParent.classList.remove(this.validClass); } else { input.classList.remove(this.validClass); } } }; FormValidator.prototype.createHTML5Rules = function () { var defRules = ['required', 'validateHidden', 'regex', 'rangeLength', 'maxLength', 'minLength', 'dateIso', 'digits', 'pattern', 'data-val-required', 'type', 'data-validation', 'min', 'max', 'range', 'equalTo', 'data-val-minlength-min', 'data-val-equalto-other', 'data-val-maxlength-max', 'data-val-range-min', 'data-val-regex-pattern', 'data-val-length-max', 'data-val-creditcard', 'data-val-phone']; var acceptedTypes = ['hidden', 'email', 'url', 'date', 'number', 'tel']; for (var _i = 0, _a = (this.inputElements); _i < _a.length; _i++) { var input = _a[_i]; // Default attribute rules var allRule = {}; for (var _b = 0, defRules_1 = defRules; _b < defRules_1.length; _b++) { var rule = defRules_1[_b]; if (input.getAttribute(rule) !== null) { switch (rule) { case 'required': this.defRule(input, allRule, rule, input.required); break; case 'data-validation': rule = input.getAttribute(rule); this.defRule(input, allRule, rule, true); break; case 'type': if (acceptedTypes.indexOf(input.type) !== -1) { this.defRule(input, allRule, input.type, true); } break; case 'rangeLength': case 'range': this.defRule(input, allRule, rule, JSON.parse(input.getAttribute(rule))); break; case 'equalTo': { var id = input.getAttribute(rule); this.defRule(input, allRule, rule, id); } break; default: if (input.getAttribute('data-val') === 'true') { this.annotationRule(input, allRule, rule, input.getAttribute(rule)); } else { this.defRule(input, allRule, rule, input.getAttribute(rule)); } } } } //adding pattern type validation if (Object.keys(allRule).length !== 0) { this.addRules(input.name, allRule); } } }; FormValidator.prototype.annotationRule = function (input, ruleCon, ruleName, value) { var annotationRule = ruleName.split('-'); var rulesList = ['required', 'creditcard', 'phone', 'maxlength', 'minlength', 'range', 'regex', 'equalto']; var ruleFirstName = annotationRule[annotationRule.length - 1]; var ruleSecondName = annotationRule[annotationRule.length - 2]; if (rulesList.indexOf(ruleFirstName) !== -1) { switch (ruleFirstName) { case 'required': this.defRule(input, ruleCon, 'required', value); break; case 'creditcard': this.defRule(input, ruleCon, 'creditcard', value); break; case 'phone': this.defRule(input, ruleCon, 'tel', value); break; } } else Eif (rulesList.indexOf(ruleSecondName) !== -1) { switch (ruleSecondName) { case 'maxlength': this.defRule(input, ruleCon, 'maxLength', value); break; case 'minlength': this.defRule(input, ruleCon, 'minLength', value); break; case 'range': { var minvalue = input.getAttribute('data-val-range-min'); var maxvalue = input.getAttribute('data-val-range-max'); this.defRule(input, ruleCon, 'range', [minvalue, maxvalue]); } break; case 'equalto': { var id = input.getAttribute(ruleName).split('.'); this.defRule(input, ruleCon, 'equalTo', id[id.length - 1]); } break; case 'regex': this.defRule(input, ruleCon, 'regex', value); break; } } }; FormValidator.prototype.defRule = function (input, ruleCon, ruleName, value) { var message = input.getAttribute('data-' + ruleName + '-message'); var annotationMessage = input.getAttribute('data-val-' + ruleName); var customMessage; if (this.rules[input.name] && ruleName !== 'validateHidden' && ruleName !== 'hidden') { this.getInputElement(input.name); customMessage = this.getErrorMessage(this.rules[input.name]["" + ruleName], ruleName); } if (message) { value = [value, message]; } else if (annotationMessage) { value = [value, annotationMessage]; } else if (customMessage) { value = [value, customMessage]; } ruleCon["" + ruleName] = value; }; // Wire events to the form elements FormValidator.prototype.wireEvents = function () { for (var _i = 0, _a = (this.inputElements); _i < _a.length; _i++) { var input = _a[_i]; if (FormValidator_1.isCheckable(input)) { ej2_base_2.EventHandler.add(input, 'click', this.clickHandler, this); } else if (input.tagName === 'SELECT') { ej2_base_2.EventHandler.add(input, 'change', this.changeHandler, this); } else { ej2_base_2.EventHandler.add(input, 'focusout', this.focusOutHandler, this); ej2_base_2.EventHandler.add(input, 'keyup', this.keyUpHandler, this); } } ej2_base_2.EventHandler.add(this.element, 'submit', this.submitHandler, this); ej2_base_2.EventHandler.add(this.element, 'reset', this.resetHandler, this); }; // UnWire events to the form elements FormValidator.prototype.unwireEvents = function () { for (var _i = 0, _a = (this.inputElements); _i < _a.length; _i++) { var input = _a[_i]; ej2_base_2.EventHandler.clearEvents(input); } ej2_base_2.EventHandler.remove(this.element, 'submit', this.submitHandler); ej2_base_2.EventHandler.remove(this.element, 'reset', this.resetHandler); }; // Handle input element focusout event FormValidator.prototype.focusOutHandler = function (e) { this.trigger('focusout', e); //FormValidator.triggerCallback(this.focusout, e); var element = e.target; if (this.rules[element.name]) { if (this.rules[element.name][this.required] || element.value.length > 0) { this.validate(element.name); } else if (this.validated.indexOf(element.name) === -1) { this.validated.push(element.name); } } }; // Handle input element keyup event FormValidator.prototype.keyUpHandler = function (e) { this.trigger('keyup', e); var element = e.target; // List of keys need to prevent while validation var excludeKeys = [16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144, 225]; if (e.which === 9 && (!this.rules[element.name] || (this.rules[element.name] && !this.rules[element.name][this.required]))) { return; } if (this.validated.indexOf(element.name) !== -1 && this.rules[element.name] && excludeKeys.indexOf(e.which) === -1) { this.validate(element.name); } }; // Handle input click event FormValidator.prototype.clickHandler = function (e) { this.trigger('click', e); var element = e.target; // If element type is not submit allow validation if (element.type !== 'submit') { this.validate(element.name); } else if (element.getAttribute('formnovalidate') !== null) { // Prevent form validation, if submit button has formnovalidate attribute this.allowSubmit = true; } }; // Handle input change event FormValidator.prototype.changeHandler = function (e) { this.trigger('change', e); var element = e.target; this.validate(element.name); }; // Handle form submit event FormValidator.prototype.submitHandler = function (e) { this.trigger('submit', e); //FormValidator.triggerCallback(this.submit, e); // Prevent form submit if validation failed if (!this.allowSubmit && !this.validate()) { e.preventDefault(); } else { this.allowSubmit = false; } }; // Handle form reset FormValidator.prototype.resetHandler = function () { this.clearForm(); }; // Validate each rule based on input element name FormValidator.prototype.validateRules = function (name) { if (!this.rules["" + name]) { return; } var rules = Object.keys(this.rules["" + name]); var hiddenType = false; var validateHiddenType = false; var vhPos = rules.indexOf('validateHidden'); var hPos = rules.indexOf('hidden'); this.getInputElement(name); if (hPos !== -1) { hiddenType = true; } if (vhPos !== -1) { validateHiddenType = true; } if (!hiddenType || (hiddenType && validateHiddenType)) { if (vhPos !== -1) { rules.splice(vhPos, 1); } if (hPos !== -1) { rules.splice((hPos - 1), 1); } this.getErrorElement(name); for (var _i = 0, rules_2 = rules; _i < rules_2.length; _i++) { var rule = rules_2[_i]; var errorMessage = this.getErrorMessage(this.rules["" + name]["" + rule], rule); var errorRule = { name: name, message: errorMessage }; var eventArgs = { inputName: name, element: this.inputElement, message: errorMessage }; if (!this.isValid(name, rule) && !this.inputElement.classList.contains(this.ignore)) { this.removeErrorRules(name); this.errorRules.push(errorRule); // Set aria attributes to invalid elements this.inputElement.setAttribute('aria-invalid', 'true'); this.inputElement.setAttribute('aria-describedby', this.inputElement.id + '-info'); var inputParent = this.inputElement.parentElement; var grandParent = inputParent.parentElement; Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper') || (this.inputElement.classList.contains('e-input') && inputParent.classList.contains('e-input-group'))) { inputParent.classList.add(this.errorClass); inputParent.classList.remove(this.validClass); } else Iif ((grandParent != null) && (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper'))) { grandParent.classList.add(this.errorClass); grandParent.classList.remove(this.validClass); } else { this.inputElement.classList.add(this.errorClass); this.inputElement.classList.remove(this.validClass); } if (!this.infoElement) { this.createErrorElement(name, errorRule.message, this.inputElement); } else { this.showMessage(errorRule); } eventArgs.errorElement = this.infoElement; eventArgs.status = 'failure'; Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper') || (this.inputElement.classList.contains('e-input') && inputParent.classList.contains('e-input-group'))) { inputParent.classList.add(this.errorClass); inputParent.classList.remove(this.validClass); } else Iif ((grandParent != null) && (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper'))) { grandParent.classList.add(this.errorClass); grandParent.classList.remove(this.validClass); } else { this.inputElement.classList.add(this.errorClass); this.inputElement.classList.remove(this.validClass); } this.optionalValidationStatus(name, eventArgs); this.trigger('validationComplete', eventArgs); // Set aria-required to required rule elements if (rule === 'required') { this.inputElement.setAttribute('aria-required', 'true'); } break; } else { this.hideMessage(name); eventArgs.status = 'success'; this.trigger('validationComplete', eventArgs); } } } else { return; } }; // Update the optional validation status FormValidator.prototype.optionalValidationStatus = function (name, refer) { if (!this.rules["" + name][this.required] && !this.inputElement.value.length && !ej2_base_2.isNullOrUndefined(this.infoElement)) { this.infoElement.innerHTML = this.inputElement.value; this.infoElement.setAttribute('aria-invalid', 'false'); refer.status = ''; this.hideMessage(name); } }; // Check the input element whether it's value satisfy the validation rule or not FormValidator.prototype.isValid = function (name, rule) { var params = this.rules["" + name]["" + rule]; var param = (params instanceof Array && typeof params[1] === 'string') ? params[0] : params; var currentRule = this.rules["" + name]["" + rule]; var dateFormat = ((rule === 'min' || rule === 'max') && this.rules['' + name].date && typeof (this.rules['' + name].date) === 'string') ? this.rules['' + name].date : null; var args = { value: this.inputElement.value, param: param, element: this.inputElement, formElement: this.element, format: dateFormat }; this.trigger('validationBegin', args); Iif (!args.param && rule === 'required') { return true; } if (currentRule && typeof currentRule[0] === 'function') { var fn = currentRule[0]; return fn.call(this, { element: this.inputElement, value: this.inputElement.value }); } else if (FormValidator_1.isCheckable(this.inputElement)) { if (rule !== 'required') { return true; } return ej2_base_1.selectAll('input[name="' + name + '"]:checked', this.element).length > 0; } else { return FormValidator_1.checkValidator["" + rule](args); } }; // Return default error message or custom error message FormValidator.prototype.getErrorMessage = function (ruleValue, rule) { var message = this.inputElement.getAttribute('data-' + rule + '-message') ? this.inputElement.getAttribute('data-' + rule + '-message') : (ruleValue instanceof Array && typeof ruleValue[1] === 'string') ? ruleValue[1] : (Object.keys(this.localyMessage).length !== 0) ? this.localyMessage["" + rule] : this.defaultMessages["" + rule]; var formats = message.match(/{(\d)}/g); if (!ej2_base_2.isNullOrUndefined(formats)) { for (var i = 0; i < formats.length; i++) { var value = ruleValue instanceof Array ? ruleValue[parseInt(i.toString(), 10)] : ruleValue; message = message.replace(formats[parseInt(i.toString(), 10)], value); } } return message; }; // Create error element based on name and error message FormValidator.prototype.createErrorElement = function (name, message, input) { var errorElement = ej2_base_1.createElement(this.errorElement, { className: this.errorClass, innerHTML: message, attrs: { for: name } }); // Create message design if errorOption is message if (this.errorOption === ErrorOption.Message) { errorElement.classList.remove(this.errorClass); errorElement.classList.add('e-message'); errorElement = ej2_base_1.createElement(this.errorContainer, { className: this.errorClass, innerHTML: errorElement.outerHTML }); } errorElement.id = this.inputElement.name + '-info'; // Append error message into MVC error message element if (this.element.querySelector('[data-valmsg-for="' + input.id + '"]')) { this.element.querySelector('[data-valmsg-for="' + input.id + '"]').appendChild(errorElement); } else if (input.hasAttribute('data-msg-containerid') === true) { // Append error message into custom div element var containerId = input.getAttribute('data-msg-containerid'); var divElement = ej2_base_1.select('#' + containerId, this.element); divElement.appendChild(errorElement); } else if (this.customPlacement != null) { // Call custom placement function if customPlacement is not null this.customPlacement.call(this, this.inputElement, errorElement); } else { var inputParent = this.inputElement.parentElement; var grandParent = inputParent.parentElement; Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper')) { grandParent.insertBefore(errorElement, inputParent.nextSibling); } else Iif (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper')) { grandParent.parentElement.insertBefore(errorElement, grandParent.nextSibling); } else { inputParent.insertBefore(errorElement, this.inputElement.nextSibling); } } errorElement.style.display = 'block'; this.getErrorElement(name); this.validated.push(name); this.checkRequired(name); }; // Get error element by name FormValidator.prototype.getErrorElement = function (name) { this.infoElement = ej2_base_1.select(this.errorElement + '.' + this.errorClass, this.inputElement.parentElement); if (!this.infoElement) { this.infoElement = ej2_base_1.select(this.errorElement + '.' + this.errorClass + '[for="' + name + '"]', this.element); } return this.infoElement; }; // Remove existing rule from errorRules object FormValidator.prototype.removeErrorRules = function (name) { for (var i = 0; i < this.errorRules.length; i++) { var rule = this.errorRules[parseInt(i.toString(), 10)]; if (rule.name === name) { this.errorRules.splice(i, 1); } } }; // Show error message to the input element FormValidator.prototype.showMessage = function (errorRule) { this.infoElement.style.display = 'block'; this.infoElement.innerHTML = errorRule.message; this.checkRequired(errorRule.name); }; // Hide error message based on input name FormValidator.prototype.hideMessage = function (name) { if (this.infoElement) { this.infoElement.style.display = 'none'; this.removeErrorRules(name); var inputParent = this.inputElement.parentElement; var grandParent = inputParent.parentElement; Iif (inputParent.classList.contains('e-control-wrapper') || inputParent.classList.contains('e-wrapper') || (this.inputElement.classList.contains('e-input') && inputParent.classList.contains('e-input-group'))) { inputParent.classList.add(this.validClass); inputParent.classList.remove(this.errorClass); } else Iif ((grandParent != null) && (grandParent.classList.contains('e-control-wrapper') || grandParent.classList.contains('e-wrapper'))) { grandParent.classList.add(this.validClass); grandParent.classList.remove(this.errorClass); } else { this.inputElement.classList.add(this.validClass); this.inputElement.classList.remove(this.errorClass); } this.inputElement.setAttribute('aria-invalid', 'false'); } }; // Check whether the input element have required rule and its value is not empty FormValidator.prototype.checkRequired = function (name) { if (!this.rules["" + name][this.required] && !this.inputElement.value.length && !ej2_base_2.isNullOrUndefined(this.infoElement)) { this.infoElement.innerHTML = this.inputElement.value; this.infoElement.setAttribute('aria-invalid', 'false'); this.hideMessage(name); } }; // Return boolean result if the input have checkable or submit types FormValidator.isCheckable = function (input) { var inputType = input.getAttribute('type'); return inputType && (inputType === 'checkbox' || inputType === 'radio' || inputType === 'submit'); }; var FormValidator_1; // List of function to validate the rules FormValidator.checkValidator = { required: function (option) { return !isNaN(Date.parse(option.value)) ? !isNaN(new Date(option.value).getTime()) : option.value.toString().length > 0; }, email: function (option) { return exports.regex.EMAIL.test(option.value); }, url: function (option) { return exports.regex.URL.test(option.value); }, dateIso: function (option) { return exports.regex.DATE_ISO.test(option.value); }, tel: function (option) { return exports.regex.PHONE.test(option.value); }, creditcard: function (option) { return exports.regex.CREDITCARD.test(option.value); }, number: function (option) { return !isNaN(Number(option.value)) && option.value.indexOf(' ') === -1; }, digits: function (option) { return exports.regex.DIGITS.test(option.value); }, maxLength: function (option) { return option.value.length <= Number(option.param); }, minLength: function (option) { return option.value.length >= Number(option.param); }, rangeLength: function (option) { var param = option.param; return option.value.length >= param[0] && option.value.length <= param[1]; }, range: function (option) { var param = option.param; return !isNaN(Number(option.value)) && Number(option.value) >= param[0] && Number(option.value) <= param[1]; }, date: function (option) { if (!ej2_base_2.isNullOrUndefined(option.param) && (typeof (option.param) === 'string' && option.param !== '')) { var globalize = new ej2_base_4.Internationalization; var dateOptions = { format: option.param.toString(), type: 'dateTime', skeleton: 'yMd' }; var dateValue = globalize.parseDate(option.value, dateOptions); return (!ej2_base_2.isNullOrUndefined(dateValue) && dateValue instanceof Date && !isNaN(+dateValue)); } else { return !isNaN(new Date(option.value).getTime()); } }, max: function (option) { if (!isNaN(Number(option.value))) { // Maximum rule validation for number return +option.value <= +option.param; } // Maximum rule validation for date if (option.format && option.format !== '') { var globalize = new ej2_base_4.Internationalization; var dateOptions = { format: option.format.toString(), type: 'dateTime', skeleton: 'yMd' }; var dateValue = globalize.parseDate(option.value, dateOptions); var maxValue = (typeof (option.param) === 'string') ? globalize.parseDate(JSON.parse(JSON.stringify(option.param)), dateOptions) : option.param; return new Date(dateValue).getTime() <= new Date(maxValue).getTime(); } else { return new Date(option.value).getTime() <= new Date(JSON.parse(JSON.stringify(option.param))).getTime(); } }, min: function (option) { if (!isNaN(Number(option.value))) { // Minimum rule validation for number return +option.value >= +option.param; } else if ((option.value).indexOf(',') !== -1) { var uNum = (option.value).replace(/,/g, ''); return parseFloat(uNum) >= Number(option.param); // Convert option.param to a number } else { // Minimum rule validation for date if (option.format && option.format !== '') { var globalize = new ej2_base_4.Internationalization; var dateOptions = { format: option.format.toString(), type: 'dateTime', skeleton: 'yMd' }; var dateValue = globalize.parseDate(option.value, dateOptions); var minValue = (typeof (option.param) === 'string') ? globalize.parseDate(JSON.parse(JSON.stringify(option.param)), dateOptions) : option.param; return new Date(dateValue).getTime() >= new Date(minValue).getTime(); } else { return new Date(option.value).getTime() >= new Date(JSON.parse(JSON.stringify(option.param))).getTime(); } } }, regex: function (option) { /* eslint-disable-next-line security/detect-non-literal-regexp */ return new RegExp(option.param).test(option.value); }, equalTo: function (option) { var compareTo = option.formElement.querySelector('#' + option.param); option.param = compareTo.value; return option.param === option.value; } }; __decorate([ ej2_base_3.Property('') ], FormValidator.prototype, "locale", void 0); __decorate([ ej2_base_3.Property('e-hidden') ], FormValidator.prototype, "ignore", void 0); __decorate([ ej2_base_3.Property() ], FormValidator.prototype, "rules", void 0); __decorate([ ej2_base_3.Property('e-error') ], FormValidator.prototype, "errorClass", void 0); __decorate([ ej2_base_3.Property('e-valid') ], FormValidator.prototype, "validClass", void 0); __decorate([ ej2_base_3.Property('label') ], FormValidator.prototype, "errorElement", void 0); __decorate([ ej2_base_3.Property('div') ], FormValidator.prototype, "errorContainer", void 0); __decorate([ ej2_base_3.Property(ErrorOption.Label) ], FormValidator.prototype, "errorOption", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "focusout", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "keyup", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "click", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "change", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "submit", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "validationBegin", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "validationComplete", void 0); __decorate([ ej2_base_3.Event() ], FormValidator.prototype, "customPlacement", void 0); FormValidator = FormValidator_1 = __decorate([ ej2_base_3.NotifyPropertyChanges ], FormValidator); return FormValidator; }(ej2_base_1.Base)); exports.FormValidator = FormValidator; }); |