|
18 | 18 | * @website http://formvalidator.net/
|
19 | 19 | * @license MIT
|
20 | 20 | */
|
21 |
| -(function($) { |
| 21 | +(function ($) { |
22 | 22 |
|
23 | 23 | 'use strict';
|
24 | 24 |
|
25 |
| - var SUPPORTS_PLACEHOLDER = 'placeholder' in document.createElement('INPUT'), |
26 |
| - SUPPORTS_DATALIST = 'options' in document.createElement('DATALIST'), |
27 |
| - hasLoadedDateModule = false, |
28 |
| - setupValidationUsingHTML5Attr = function($form) { |
29 |
| - |
30 |
| - $form.each(function() { |
31 |
| - var $f = $(this), |
32 |
| - $formInputs = $f.find('input,textarea,select'), |
33 |
| - foundHtml5Rule = false; |
34 |
| - |
35 |
| - $formInputs.each(function() { |
36 |
| - var validation = [], |
37 |
| - $input = $(this), |
38 |
| - isRequired = $input.attr('required'), |
39 |
| - attrs = {}; |
40 |
| - |
41 |
| - if (isRequired) { |
42 |
| - validation.push('required'); |
43 |
| - } |
44 |
| - |
45 |
| - switch ( ($input.attr('type') || '').toLowerCase() ) { |
46 |
| - case 'time': |
47 |
| - validation.push('time'); |
48 |
| - if( !$.formUtils.validators.validate_date && !hasLoadedDateModule ) { |
49 |
| - hasLoadedDateModule = true; |
50 |
| - $.formUtils.loadModules('date'); |
51 |
| - } |
52 |
| - break; |
53 |
| - case 'url': |
54 |
| - validation.push('url'); |
55 |
| - break; |
56 |
| - case 'email': |
57 |
| - validation.push('email'); |
58 |
| - break; |
59 |
| - case 'date': |
60 |
| - validation.push('date'); |
61 |
| - break; |
62 |
| - case 'number': |
63 |
| - validation.push('number'); |
64 |
| - var max = $input.attr('max'), |
65 |
| - min = $input.attr('min'), |
66 |
| - step = $input.attr('step'); |
67 |
| - if( min || max ) { |
68 |
| - if ( !min ) { |
69 |
| - min = '0'; |
70 |
| - } |
71 |
| - if ( !max ) { |
72 |
| - max = '9007199254740992'; // js max int |
73 |
| - } |
74 |
| - if ( !step ) { |
75 |
| - step = '1'; // default value |
76 |
| - } |
77 |
| - |
78 |
| - attrs['data-validation-allowing'] = 'range[' +min+';'+max+']'; |
79 |
| - if( min.indexOf('-') === 0 || max.indexOf('-') === 0 ) { |
80 |
| - attrs['data-validation-allowing'] += ',negative'; |
81 |
| - } |
82 |
| - if( min.indexOf('.') > -1 || max.indexOf('.') > -1 || step.indexOf('.') > -1 ) { |
83 |
| - attrs['data-validation-allowing'] += ',float'; |
84 |
| - } |
85 |
| - } else { |
86 |
| - attrs['data-validation-allowing'] += ',float,negative'; |
87 |
| - } |
88 |
| - break; |
| 25 | + var SUPPORTS_PLACEHOLDER = 'placeholder' in document.createElement('INPUT'), |
| 26 | + SUPPORTS_DATALIST = 'options' in document.createElement('DATALIST'), |
| 27 | + hasLoadedDateModule = false, |
| 28 | + setupValidationUsingHTML5Attr = function ($form) { |
| 29 | + |
| 30 | + $form.each(function () { |
| 31 | + var $f = $(this), |
| 32 | + $formInputs = $f.find('input,textarea,select'), |
| 33 | + foundHtml5Rule = false; |
| 34 | + |
| 35 | + $formInputs.each(function () { |
| 36 | + var validation = [], |
| 37 | + $input = $(this), |
| 38 | + isRequired = $input.attr('required'), |
| 39 | + attrs = {}; |
| 40 | + |
| 41 | + if (isRequired) { |
| 42 | + validation.push('required'); |
| 43 | + } |
| 44 | + |
| 45 | + switch (($input.attr('type') || '').toLowerCase()) { |
| 46 | + case 'time': |
| 47 | + validation.push('time'); |
| 48 | + if (!$.formUtils.validators.validate_date && !hasLoadedDateModule) { |
| 49 | + hasLoadedDateModule = true; |
| 50 | + $.formUtils.loadModules('date'); |
89 | 51 | }
|
| 52 | + break; |
| 53 | + case 'url': |
| 54 | + validation.push('url'); |
| 55 | + break; |
| 56 | + case 'email': |
| 57 | + validation.push('email'); |
| 58 | + break; |
| 59 | + case 'date': |
| 60 | + validation.push('date'); |
| 61 | + break; |
| 62 | + case 'number': |
| 63 | + validation.push('number'); |
| 64 | + var max = $input.attr('max'), |
| 65 | + min = $input.attr('min'), |
| 66 | + step = $input.attr('step'); |
| 67 | + if (min || max) { |
| 68 | + if (!min) { |
| 69 | + min = '0'; |
| 70 | + } |
| 71 | + if (!max) { |
| 72 | + max = '9007199254740992'; // js max int |
| 73 | + } |
| 74 | + if (!step) { |
| 75 | + step = '1'; // default value |
| 76 | + } |
90 | 77 |
|
91 |
| - if( $input.attr('pattern') ) { |
92 |
| - validation.push('custom'); |
93 |
| - attrs['data-validation-regexp'] = $input.attr('pattern'); |
94 |
| - } |
95 |
| - if( $input.attr('maxlength') ) { |
96 |
| - validation.push('length'); |
97 |
| - attrs['data-validation-length'] = 'max'+$input.attr('maxlength'); |
| 78 | + attrs['data-validation-allowing'] = 'range[' + min + ';' + max + ']'; |
| 79 | + if (min.indexOf('-') === 0 || max.indexOf('-') === 0) { |
| 80 | + attrs['data-validation-allowing'] += ',negative'; |
| 81 | + } |
| 82 | + if (min.indexOf('.') > -1 || max.indexOf('.') > -1 || step.indexOf('.') > -1) { |
| 83 | + attrs['data-validation-allowing'] += ',float'; |
| 84 | + } |
| 85 | + } else { |
| 86 | + attrs['data-validation-allowing'] += ',float,negative'; |
98 | 87 | }
|
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + if ($input.attr('pattern')) { |
| 92 | + validation.push('custom'); |
| 93 | + attrs['data-validation-regexp'] = $input.attr('pattern'); |
| 94 | + } |
| 95 | + if ($input.attr('maxlength')) { |
| 96 | + validation.push('length'); |
| 97 | + attrs['data-validation-length'] = 'max' + $input.attr('maxlength'); |
| 98 | + } |
| 99 | + |
| 100 | + if (!SUPPORTS_DATALIST && $input.attr('list')) { |
| 101 | + var suggestions = [], |
| 102 | + $list = $('#' + $input.attr('list')); |
| 103 | + |
| 104 | + $list.find('option').each(function () { |
| 105 | + suggestions.push($(this).text()); |
| 106 | + }); |
99 | 107 |
|
100 |
| - if( !SUPPORTS_DATALIST && $input.attr('list') ) { |
101 |
| - var suggestions = [], |
102 |
| - $list = $('#'+$input.attr('list')); |
103 |
| - |
104 |
| - $list.find('option').each(function() { |
105 |
| - suggestions.push($(this).text()); |
106 |
| - }); |
107 |
| - |
108 |
| - if( suggestions.length === 0 ) { |
109 |
| - // IE fix |
110 |
| - var opts = $.trim($('#'+$input.attr('list')).text()).split('\n'); |
111 |
| - $.each(opts, function(i, option) { |
112 |
| - suggestions.push($.trim(option)); |
113 |
| - }); |
114 |
| - } |
| 108 | + if (suggestions.length === 0) { |
| 109 | + // IE fix |
| 110 | + var opts = $.trim($('#' + $input.attr('list')).text()).split('\n'); |
| 111 | + $.each(opts, function (i, option) { |
| 112 | + suggestions.push($.trim(option)); |
| 113 | + }); |
| 114 | + } |
115 | 115 |
|
116 |
| - $list.remove(); |
| 116 | + $list.remove(); |
117 | 117 |
|
118 |
| - $.formUtils.suggest( $input, suggestions ); |
119 |
| - } |
| 118 | + $.formUtils.suggest($input, suggestions); |
| 119 | + } |
120 | 120 |
|
121 |
| - if( validation.length ) { |
122 |
| - if( !isRequired ) { |
123 |
| - attrs['data-validation-optional'] = 'true'; |
124 |
| - } |
| 121 | + if (validation.length) { |
| 122 | + if (!isRequired) { |
| 123 | + attrs['data-validation-optional'] = 'true'; |
| 124 | + } |
125 | 125 |
|
126 |
| - foundHtml5Rule = true; |
| 126 | + foundHtml5Rule = true; |
127 | 127 |
|
128 |
| - var validationRules = ($input.attr('data-validation') || '') +' '+ validation.join(' '); |
129 |
| - $input.attr('data-validation', $.trim(validationRules)); |
| 128 | + var validationRules = ($input.attr('data-validation') || '') + ' ' + validation.join(' '); |
| 129 | + $input.attr('data-validation', $.trim(validationRules)); |
130 | 130 |
|
131 |
| - $.each(attrs, function(attrName, attrVal) { |
132 |
| - $input.attr(attrName, attrVal); |
133 |
| - }); |
134 |
| - } |
| 131 | + $.each(attrs, function (attrName, attrVal) { |
| 132 | + $input.attr(attrName, attrVal); |
135 | 133 | });
|
| 134 | + } |
| 135 | + }); |
136 | 136 |
|
137 |
| - if( foundHtml5Rule ) { |
138 |
| - $f.trigger('html5ValidationAttrsFound'); |
139 |
| - } |
| 137 | + if (foundHtml5Rule) { |
| 138 | + $f.trigger('html5ValidationAttrsFound'); |
| 139 | + } |
140 | 140 |
|
141 |
| - if( !SUPPORTS_PLACEHOLDER ) { |
142 |
| - $formInputs.filter('input[placeholder]').each(function() { |
143 |
| - this.defaultValue = this.getAttribute('placeholder'); |
144 |
| - $(this) |
145 |
| - .bind('focus', function() { |
146 |
| - if(this.value === this.defaultValue) { |
147 |
| - this.value = ''; |
148 |
| - $(this).removeClass('showing-placeholder'); |
149 |
| - } |
150 |
| - }) |
151 |
| - .bind('blur', function() { |
152 |
| - if($.trim(this.value) === '') { |
153 |
| - this.value = this.defaultValue; |
154 |
| - $(this).addClass('showing-placeholder'); |
155 |
| - } |
156 |
| - }); |
| 141 | + if (!SUPPORTS_PLACEHOLDER) { |
| 142 | + $formInputs.filter('input[placeholder]').each(function () { |
| 143 | + this.__defaultValue = this.getAttribute('placeholder'); |
| 144 | + $(this) |
| 145 | + .bind('focus', function () { |
| 146 | + if (this.value === this.__defaultValue) { |
| 147 | + this.value = ''; |
| 148 | + $(this).removeClass('showing-placeholder'); |
| 149 | + } |
| 150 | + }) |
| 151 | + .bind('blur', function () { |
| 152 | + if ($.trim(this.value) === '') { |
| 153 | + this.value = this.__defaultValue; |
| 154 | + $(this).addClass('showing-placeholder'); |
| 155 | + } |
157 | 156 | });
|
158 |
| - } |
159 |
| - |
160 | 157 | });
|
161 |
| - }; |
162 |
| - |
163 |
| - $.formUtils.$win.bind('validatorsLoaded formValidationSetup', function(evt, $form) { |
164 |
| - if( !$form ) { |
165 |
| - $form = $('form'); |
166 | 158 | }
|
167 |
| - setupValidationUsingHTML5Attr($form); |
168 |
| - }); |
169 | 159 |
|
170 |
| - // Make this method available outside the module |
171 |
| - $.formUtils.setupValidationUsingHTML5Attr = setupValidationUsingHTML5Attr; |
| 160 | + }); |
| 161 | + }; |
| 162 | + |
| 163 | + $.formUtils.$win.bind('validatorsLoaded formValidationSetup', function (evt, $form) { |
| 164 | + if (!$form) { |
| 165 | + $form = $('form'); |
| 166 | + } |
| 167 | + setupValidationUsingHTML5Attr($form); |
| 168 | + }); |
| 169 | + |
| 170 | + // Make this method available outside the module |
| 171 | + $.formUtils.setupValidationUsingHTML5Attr = setupValidationUsingHTML5Attr; |
172 | 172 |
|
173 | 173 | })(jQuery, window);
|
0 commit comments