| 5048 |
dpurdie |
1 |
/**
|
|
|
2 |
* jQuery Form Validator Module: html5
|
|
|
3 |
* ------------------------------------------
|
|
|
4 |
* Created by Victor Jonsson <http://www.victorjonsson.se>
|
|
|
5 |
*
|
|
|
6 |
* The following module will make this jQuery plugin serve as a
|
|
|
7 |
* html5 fallback. It makes older browsers support the following
|
|
|
8 |
* - validation when type="email"
|
|
|
9 |
* - validation when type="url"
|
|
|
10 |
* - validation when type="time"
|
|
|
11 |
* - validation when type="date"
|
|
|
12 |
* - validation when type="number" and max="" min=""
|
|
|
13 |
* - validation when pattern="REGEXP"
|
|
|
14 |
* - validation when using maxlength
|
|
|
15 |
* - Using datalist element for creating suggestions
|
|
|
16 |
* - placeholders
|
|
|
17 |
*
|
|
|
18 |
* @website http://formvalidator.net/
|
|
|
19 |
* @license Dual licensed under the MIT or GPL Version 2 licenses
|
|
|
20 |
* @version 2.2.beta.69
|
|
|
21 |
*/
|
|
|
22 |
(function($, window) {
|
|
|
23 |
|
|
|
24 |
"use strict";
|
|
|
25 |
|
|
|
26 |
var SUPPORTS_PLACEHOLDER = 'placeholder' in document.createElement('INPUT'),
|
|
|
27 |
SUPPORTS_DATALIST = 'options' in document.createElement('DATALIST');
|
|
|
28 |
|
|
|
29 |
$(window).bind('validatorsLoaded formValidationSetup', function(evt, $form) {
|
|
|
30 |
|
|
|
31 |
if( !$form ) {
|
|
|
32 |
$form = $('form');
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
var hasLoadedDateModule = false;
|
|
|
36 |
|
|
|
37 |
$form.each(function() {
|
|
|
38 |
var $f = $(this),
|
|
|
39 |
$formInputs = $f.find('input,textarea,select'),
|
|
|
40 |
foundHtml5Rule = false;
|
|
|
41 |
|
|
|
42 |
$formInputs.each(function() {
|
|
|
43 |
var validation = [],
|
|
|
44 |
$input = $(this),
|
|
|
45 |
isRequired = $input.attr('required'),
|
|
|
46 |
attrs = {};
|
|
|
47 |
|
|
|
48 |
switch ( ($input.attr('type') || '').toLowerCase() ) {
|
|
|
49 |
case 'time':
|
|
|
50 |
validation.push('time');
|
|
|
51 |
if( !$.formUtils.validators.validate_date && !hasLoadedDateModule ) {
|
|
|
52 |
hasLoadedDateModule = true;
|
|
|
53 |
$.formUtils.loadModules('date');
|
|
|
54 |
}
|
|
|
55 |
break;
|
|
|
56 |
case 'url':
|
|
|
57 |
validation.push('url');
|
|
|
58 |
break;
|
|
|
59 |
case 'email':
|
|
|
60 |
validation.push('email');
|
|
|
61 |
break;
|
|
|
62 |
case 'date':
|
|
|
63 |
validation.push('date');
|
|
|
64 |
break;
|
|
|
65 |
case 'number':
|
|
|
66 |
validation.push('number');
|
|
|
67 |
var max = $input.attr('max'),
|
|
|
68 |
min = $input.attr('min');
|
|
|
69 |
if( min || max ) {
|
|
|
70 |
if( !min )
|
|
|
71 |
min = 0;
|
|
|
72 |
if( !max )
|
|
|
73 |
max = 9007199254740992; // js max int
|
|
|
74 |
|
|
|
75 |
attrs['data-validation-allowing'] = 'range['+min+';'+max+']';
|
|
|
76 |
if( min.indexOf('-') === 0 || max.indexOf('-') === 0 ) {
|
|
|
77 |
attrs['data-validation-allowing'] += ',negative';
|
|
|
78 |
}
|
|
|
79 |
if( min.indexOf('.') > -1 || max.indexOf('.') > -1 ) {
|
|
|
80 |
attrs['data-validation-allowing'] += ',float';
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
break;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if( $input.attr('pattern') ) {
|
|
|
87 |
validation.push('custom');
|
|
|
88 |
attrs['data-validation-regexp'] = $input.attr('pattern');
|
|
|
89 |
}
|
|
|
90 |
if( $input.attr('maxlength') ) {
|
|
|
91 |
validation.push('length');
|
|
|
92 |
attrs['data-validation-length'] = 'max'+$input.attr('maxlength');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if( !SUPPORTS_DATALIST && $input.attr('list') ) {
|
|
|
96 |
var suggestions = [];
|
|
|
97 |
$('#'+$input.attr('list')+' option').each(function() {
|
|
|
98 |
var $opt = $(this);
|
|
|
99 |
suggestions.push($opt.attr('value') || $opt.text());
|
|
|
100 |
});
|
|
|
101 |
$.formUtils.suggest( $input, suggestions );
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if( validation.length ) {
|
|
|
105 |
if( !isRequired ) {
|
|
|
106 |
attrs['data-validation-optional'] = 'true';
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
foundHtml5Rule = true;
|
|
|
110 |
$input.attr('data-validation', validation.join(' '));
|
|
|
111 |
|
|
|
112 |
$.each(attrs, function(attrName, attrVal) {
|
|
|
113 |
$input.attr(attrName, attrVal);
|
|
|
114 |
});
|
|
|
115 |
}
|
|
|
116 |
});
|
|
|
117 |
|
|
|
118 |
if( foundHtml5Rule ) {
|
|
|
119 |
$f.trigger('html5ValidationAttrsFound');
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if( !SUPPORTS_PLACEHOLDER ) {
|
|
|
123 |
$formInputs.filter('input[placeholder]').each(function() {
|
|
|
124 |
this.defaultValue = this.getAttribute('placeholder');
|
|
|
125 |
$(this)
|
|
|
126 |
.bind('focus', function() {
|
|
|
127 |
if(this.value == this.defaultValue) {
|
|
|
128 |
this.value = '';
|
|
|
129 |
$(this).removeClass('showing-placeholder');
|
|
|
130 |
}
|
|
|
131 |
})
|
|
|
132 |
.bind('blur', function() {
|
|
|
133 |
if($.trim(this.value) == '') {
|
|
|
134 |
this.value = this.defaultValue;
|
|
|
135 |
$(this).addClass('showing-placeholder');
|
|
|
136 |
}
|
|
|
137 |
});
|
|
|
138 |
});
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
});
|
|
|
142 |
});
|
|
|
143 |
|
|
|
144 |
})(jQuery, window);
|