1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . default = void 0 ;
7+
8+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
9+
10+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
11+
12+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
13+
14+ var html5formValidation =
15+ /*#__PURE__*/
16+ function ( ) {
17+ function html5formValidation ( ) {
18+ var element = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : document . querySelector ( 'form' ) ;
19+ var settings = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : { } ;
20+
21+ _classCallCheck ( this , html5formValidation ) ;
22+
23+ this . settings = Object . assign ( { } , {
24+ errorElement : 'error' ,
25+ invalidClass : 'invalid' ,
26+ submitHandler : null ,
27+ validateOnInput : true
28+ } , settings ) ;
29+ this . form = element ;
30+ this . errorDiv = "<div class=\"" . concat ( this . settings . errorElement , "\"></div>" ) ;
31+ this . init ( ) ;
32+ }
33+
34+ _createClass ( html5formValidation , [ {
35+ key : "init" ,
36+ value : function init ( ) {
37+ var _this = this ;
38+
39+ this . form . noValidate = true ;
40+ this . settings . validateOnInput && this . validateAll ( this . form ) ;
41+
42+ this . form . onsubmit = function ( event ) {
43+ _this . validateAll ( _this . form ) ;
44+
45+ if ( ! _this . form . checkValidity ( ) ) {
46+ event . preventDefault ( ) ;
47+ } else {
48+ if ( typeof _this . settings . submitHandler === 'function' ) {
49+ event . preventDefault ( ) ;
50+
51+ _this . settings . submitHandler ( _this ) ;
52+ }
53+ }
54+ } ;
55+ }
56+ } , {
57+ key : "validateAll" ,
58+ value : function validateAll ( form ) {
59+ var requiredFields = form . querySelectorAll ( '[required]' ) ;
60+ var _iteratorNormalCompletion = true ;
61+ var _didIteratorError = false ;
62+ var _iteratorError = undefined ;
63+
64+ try {
65+ for ( var _iterator = requiredFields [ Symbol . iterator ] ( ) , _step ; ! ( _iteratorNormalCompletion = ( _step = _iterator . next ( ) ) . done ) ; _iteratorNormalCompletion = true ) {
66+ var _field = _step . value ;
67+ this . validateField ( _field ) ;
68+ }
69+ } catch ( err ) {
70+ _didIteratorError = true ;
71+ _iteratorError = err ;
72+ } finally {
73+ try {
74+ if ( ! _iteratorNormalCompletion && _iterator . return != null ) {
75+ _iterator . return ( ) ;
76+ }
77+ } finally {
78+ if ( _didIteratorError ) {
79+ throw _iteratorError ;
80+ }
81+ }
82+ }
83+ }
84+ } , {
85+ key : "validateField" ,
86+ value : function validateField ( field ) {
87+ var _this2 = this ;
88+
89+ if ( ! ( field . nextSibling . classList && field . nextSibling . classList . contains ( this . settings . errorElement ) ) ) {
90+ field . insertAdjacentHTML ( 'afterend' , this . errorDiv ) ;
91+ }
92+
93+ field . oninvalid = function ( ) {
94+ field . classList . add ( _this2 . settings . invalidClass ) ;
95+ field . nextSibling . textContent = field . validationMessage ;
96+ } ;
97+
98+ field . oninput = function ( ) {
99+ field . nextSibling . textContent = '' ;
100+ field . classList . remove ( _this2 . settings . invalidClass ) ;
101+ field . checkValidity ( ) ;
102+ } ;
103+ }
104+ } ] ) ;
105+
106+ return html5formValidation ;
107+ } ( ) ;
108+
109+ exports . default = html5formValidation ;
0 commit comments