This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.easy-validation.js
264 lines (230 loc) · 8.96 KB
/
jquery.easy-validation.js
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
/*! Easy Validation (c) Aaron Gustafson (@AaronGustafson). MIT License. http://github.com/easy-designs/jquery.easy-validation.js */
/* Easy Validation API
*
* Our validation strategy involves using an HTML5-aware browser’s built-in
* validation mechanism, falling back to a JavaScript-based validation scheme
* in non HTML5-aware browsers, and finally degrading to server-side validation
* in non-JavaScript scenarios. HTML5 supports field validation using a few
* mechanisms, two of which we commonly use:
*
* - The required attribute indicates a field is required
* - The pattern attribute allows you to indicate a regular expression for matching
* against the supplied content
*
* Using a few custom data-* attributes, you can tailor the error messages
* provided to users via browser or JavaScript based validation for forms as a
* whole or individual fields:
*
* - The data-validation-error-empty attribute
* When used on a form, it prescribes a global default message for all fields
* that are invalid when empty; when used on a field, it provides a field-specific
* error message when empty.
* - The data-validation-error-invalid attribute
* When used on a form, it prescribes a global default message for all fields that
* are invalid; when used on a field, it provides a field-specific error message when invalid.
*
**/
(function( $, document, window, UA, UNDEFINED ){
var FALSE = false,
TRUE = true,
$forms = $('form'),
ERROR = 'validation-error',
EMPTY = ERROR + '-empty',
INVALID = ERROR + '-invalid',
DEFAULT_EMPTY_MSG = 'This field cannot be left blank',
DEFAULT_INVALID_MSG = 'This field is not properly formatted',
container = 'form > ol > li, fieldset > ol > li',
// borrowed from Modernizr
html5_validation = (function( props ){
var supported = {},
input = document.createElement('input');
// IE8+ fibs. This test ensures it reports false
if ( typeof(input.setCustomValidity) != 'function' )
{
return FALSE;
}
$.each( props, function( i, prop ){
if ( ! ( 'hasOwnProperty' in input && input.hasOwnProperty( prop ) ) )
{
return FALSE;
}
else
{
supported[prop] = TRUE;
}
});
// Android & Safari (inc iOS) have a tendency to lie
// for now this is the best we can do
if ( supported.pattern && supported.required &&
( UA.indexOf('android') != -1 || ( UA.indexOf('applewebkit') != -1 && UA.indexOf('chrome') == -1 ) ) )
{
return FALSE;
}
return TRUE;
})('max min pattern required step'.split(' ')),
// Patterns on loan from h5 Validate (https://github.com/dilvie/h5Validate)
// not using them yet though
patterns = {
tel: /([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/,
email: /((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\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(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?/,
number: /-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?/,
dateISO: /\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,
alpha: /[a-zA-Z]+/,
alphaNumeric: /\w+/,
integer: /-?\d+/
};
// Validation algorithm
$.fn.validate = function(){
$(this).on( 'submit', function( evt ){
var $form = $(this),
$error = $('<strong class="error-message"/>'),
empty = $form.data(EMPTY) || DEFAULT_EMPTY_MSG,
invalid = $form.data(INVALID) || DEFAULT_INVALID_MSG,
error = FALSE, // optimism
$first = $([]);
$form
// cleanup
.find( '.' + ERROR )
.children('strong.error-message')
.remove()
.end()
.removeClass( ERROR )
.end()
// validation
.find('input,select,textarea')
.each(function(){
var $field = $(this),
$container = $field.closest( container ),
val = $field.val(),
type = $field.attr('type'),
r, e;
// skip already errored groups
if ( $field.is(':checkbox,:radio') &&
$container.hasClass( ERROR ) )
{
return;
}
// additional value checks for multi-selects and radios & checkboxes
switch ( TRUE )
{
case $field.is('select[multiple]'):
val = $field.find('option:selected').val();
break;
case $field.is(':checkbox'):
val = $('[name^=' + $field.attr('name').replace('[]','') + ']:checked').val();
break;
case $field.is(':radio'):
val = $('[name=' + $field.attr('name') + ']:checked').val();
break;
}
if ( val == UNDEFINED )
{
val = '';
}
if ( $field.is('[required]') && val == '' )
{
e = $field.data( EMPTY ) || empty;
$error
.clone()
.text(e)
.appendTo(
$container.addClass( ERROR )
);
error = TRUE;
if ( $first.length < 1 )
{
$first = $field;
}
}
// not required or not empty
else
{
if ( $field.is('[pattern]') )
{
r = new RegExp( $field.attr('pattern') );
}
else if ( patterns[type] != UNDEFINED )
{
r = patterns[type];
}
if ( r )
{
e = $field.data(INVALID) || invalid;
if ( ! val.match( r ) )
{
$error
.clone()
.text(e)
.appendTo(
$container.addClass( ERROR )
);
error = TRUE;
if ( $first.length < 1 )
{
$first = $field;
}
}
}
}
});
// scroll to the first error
if ( $first.length )
{
$(window).scrollTop( $first.offset().top );
}
return ! error;
});
return this;
};
if ( $forms.length )
{
if ( html5_validation )
{
// console.log('html 5');
/* TELEPHONE
* browsers aren’t instructed to validate phone #s
* so we’ll pick up the slack (provided a manual
* pattern is not in use)
*/
$('[type=tel]:not([pattern])').each(function(){
$(this).attr(
'pattern',
patterns['tel'].toString().replace(/\//g,'')
);
});
// custom error messaging
$( 'input,select,textarea' )
// handle invalidity
.on( 'invalid', function(){
var $field = $(this),
$form = $field.closest('form'),
empty = $form.data(EMPTY) || DEFAULT_EMPTY_MSG,
invalid = $form.data(INVALID) || DEFAULT_INVALID_MSG,
msg = '';
if ( this.validity.valueMissing )
{
msg = $field.data( EMPTY ) || empty;
}
else if ( ! this.validity.valid )
{
msg = $field.data( INVALID ) || invalid;
}
$field.closest( container )
.addClass( ERROR );
this.setCustomValidity( msg );
})
// reset
.on( 'change', function(){
$(this).closest( container )
.removeClass( ERROR );
this.setCustomValidity('');
});
}
else
{
//console.log('no html5 :-(');
$forms.validate();
}
}
})( jQuery, document, window, navigator.userAgent.toLowerCase() );