forked from ericelliott/h5Validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.h5validate.js
executable file
·319 lines (285 loc) · 12.8 KB
/
jquery.h5validate.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
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
/**
* h5Validate
* @version v0.4.0
* Using semantic versioning: http://semver.org/
* @author Eric Hamilton [email protected]
* @copyright 2010 Eric Hamilton
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Developed under the sponsorship of Zumba Fitness, LLC, and Rese Property Management
*/
/*global jQuery window */
/*jslint browser: true, devel: true, onevar: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
(function ($) {
var h5 = { // Public API
defaults : {
debug: true,
// HTML5-compatible validation pattern library that can be extended and/or overriden.
patternLibrary : { //** TODO: Test the new regex patterns. Should I apply these to the new input types?
// **TODO: url, password
phone: /([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/,
// Shamelessly lifted from Scott Gonzalez via the Bassistance Validation plugin http://projects.scottsplayground.com/email_address_validation/
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])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\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])|(([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])))\.?/,
// Shamelessly lifted from Scott Gonzalez via the Bassistance Validation plugin http://projects.scottsplayground.com/iri/
url: /(https?|ftp):\/\/(((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\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])|(([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, including positive, negative, and floating decimal. Credit: bassistance
number: /-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?/,
// Date in ISO format. Credit: bassistance
dateISO: /\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,
alpha: /[a-zA-Z]+/,
alphaNumeric: /\w+/,
integer: /-?\d+/
},
messages : {
required: 'This is a required field.',
invalid: 'Please correct this field.'
},
// The prefix to use for dynamically-created class names.
classPrefix: 'h5-',
errorClass: 'ui-state-error', // No prefix for these.
validClass: 'ui-state-valid', // "
activeClass: 'active', // Prefix will get prepended.
// Attribute which stores the ID of the error container element (without the hash).
errorAttribute: 'data-h5-errorid',
// Setup KB event delegation.
kbSelectors: ':text, :password, select, textarea',
focusout: true,
focusin: false,
change: true,
keyup: false,
// Setup mouse event delegation.
mSelectors: ':radio, :checkbox, select, option',
click: true,
activeKeyup: true,
// Validate on submit?
// **TODO: This isn't implemented, yet.
submit: true,
// Mark field invalid.
// ** TODO: Highlight labels
// **TODO: Simplify function signature: element, reason, settings -- was element, reason, errorClass, validClass, errorID, settings
// ** TODO: Implement setCustomValidity as per the spec:
// http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-setcustomvalidity
markInvalid: function (options) {
var $element = $(options.element),
$errorID = $(options.errorID);
$element.addClass(options.errorClass).removeClass(options.validClass);
// User needs help. Enable active validation.
$element.addClass(options.settings.activeClass);
if ($errorID.length) { // These ifs are technically not needed, but improve server-side performance
if ($element.attr('title')) {
$errorID.text($element.attr('title'));
}
$errorID.show();
}
return $element;
},
// Mark field valid.
// **TODO: Simplify function signature: element, reason, settings -- was element, reason, errorClass, validClass, errorID, settings
markValid: function (element, errorClass, validClass, errorID) {
var $element = $(element),
$errorID = $(errorID);
$element.addClass(validClass).removeClass(errorClass);
if ($errorID.length) {
$errorID.hide();
}
return $element;
},
// Unmark field
// **TODO: Simplify function signature: element, settings -- was element, reason, errorClass, validClass, errorID, settings
unmark: function (element, errorClass, validClass, errorID) {
var $element = $(element);
$element.removeClass(errorClass).removeClass(validClass);
$element.form.find("#" + element.id).removeClass(errorClass).removeClass(validClass);
return $element;
}
}
},
// Aliases
defaults = h5.defaults,
messages = defaults.messages,
patternLibrary = defaults.patternLibrary,
methods = {
validate: function (settings) {
// Get the HTML5 pattern attribute if it exists.
// ** TODO: If a pattern class exists, grab the pattern from the patternLibrary, but the pattern attrib should override that value.
var $this = $(this),
pattern = $this.filter('[pattern]')[0] ? $this.attr('pattern') : false,
// The pattern attribute must match the whole value, not just a subset:
// "...as if it implied a ^(?: at the start of the pattern and a )$ at the end."
re = new RegExp('^(?:' + pattern + ')$'),
value = $this.val(),
errorClass = settings.errorClass,
validClass = settings.validClass,
errorIDbare = $this.attr(settings.errorAttribute) || false, // Get the ID of the error element.
errorID = errorIDbare ? '#' + errorIDbare : false, // Add the hash for convenience. This is done in two steps to avoid two attribute lookups.
required = false,
isValid = true,
reason = '',
$checkRequired = $('<input required>');
/* If the required attribute exists, set it required to true, unless it's set 'false'.
* This is a minor deviation from the spec, but it seems some browsers have falsey
* required values if the attribute is empty (should be true). The more conformant
* version of this failed sanity checking in the browser environment.
* This plugin is meant to be practical, not ideologically married to the spec.
*/
// Feature fork
if ($checkRequired.filter('[required]') && $checkRequired.filter('[required]').length) {
required = ($this.filter('[required]').length && $this.attr('required') !== 'false') ? true : false;
} else {
required = ($this.attr('required') !== undefined) ? true : false;
}
if (settings.debug && window.console) {
console.log('Validate called on "' + value + '" with regex "' + re + '". Required: ' + required); // **DEBUG
console.log('Regex test: ' + re.test(value) + ', Pattern: ' + pattern); // **DEBUG
}
if (required && !value) {
isValid = false;
reason = 'required';
} else if (pattern && !re.test(value) && value) {
isValid = false;
reason = 'pattern';
} else {
isValid = true;
settings.markValid(this, errorClass, validClass, errorID);
}
if (!isValid) {
settings.markInvalid({
element: this,
reason: reason,
errorClass: errorClass,
validClass: validClass,
errorID: errorID,
settings: settings
});
}
},
/**
* Take the event preferences and delegate the events to selected
* objects.
*
* @param {object} eventFlags The object containing event flags.
*
* @returns {element} The passed element (for method chaining).
*/
delegateEvents: function (selectors, eventFlags, element, settings) {
var events = [],
key = 0,
validate = function () {
methods.validate.call(this, settings);
};
$.each(eventFlags, function (key, value) {
if (value) {
events[key] = key;
}
});
key=0;
for (key in events) {
if (events.hasOwnProperty(key)) {
if (settings.debug && window.console) {
console.log(events[key] + ', ' + selectors); //**DEBUG
}
$(element).delegate(selectors, events[key] + '.h5Validate', validate);
}
}
return element;
},
/**
* Prepare for event delegation.
*
* @param {object} settings The full plugin state, including
* options.
*
* @returns {object} jQuery object for chaining.
*/
bindDelegation: function (settings) {
// Attach patterns from the library to elements.
$.each(patternLibrary, function (key, value) {
var pattern = value.toString();
pattern = pattern.substring(1, pattern.length-1);
if (settings.debug && window.console) {
console.log('.' + settings.classPrefix + key + ' : ' + pattern);
}
$('.' + settings.classPrefix + key).attr('pattern', pattern);
});
return this.each(function () {
var kbEvents = {
focusout: settings.focusout,
focusin: settings.focusin,
change: settings.change,
keyup: settings.keyup
},
mEvents = {
click: settings.click
},
activeEvents = {
keyup:settings.activeKeyup
};
methods.delegateEvents(settings.kbSelectors, kbEvents, this, settings);
methods.delegateEvents(settings.mSelectors, mEvents, this, settings);
methods.delegateEvents(settings.activeClassSelector, activeEvents, this, settings);
});
}
};
$.h5Validate = {
/**
* Take a map of pattern names and HTML5-compatible regular
* expressions, and add them to the patternLibrary. Patterns in
* the library are automatically assigned to HTML element pattern
* attributes for validation.
*
* @param {Object} patterns A map of pattern names and HTML5 compatible
* regular expressions.
*
* @returns {Object} patternLibrary The modified pattern library
*/
addPatterns : function (patterns) {
var patternLibrary = defaults.patternLibrary,
key;
for (key in patterns) {
if (patterns.hasOwnProperty(key)) {
patternLibrary[key] = patterns[key];
if (defaults.debug && window.console) {
console.log(patternLibrary[key]);
}
}
}
return patternLibrary;
},
/**
* Take a valid jQuery selector, and a list of valid values to
* validate against.
* If the user input isn't in the list, validation fails.
*
* @param {String} selector Any valid jQuery selector.
*
* @param {Array} values A list of valid values to validate selected
* fields against.
*/
validValues : function (selector, values) {
var i = 0,
ln = values.length,
pattern = '';
// Build regex pattern
for (i = 0; i < ln; i++) {
pattern = pattern ? pattern + '|' + values[i] : values[i];
}
$(selector).attr('pattern', pattern);
}
};
$.fn.h5Validate = function (options) {
// Combine defaults and options to get current settings.
var settings = $.extend({}, defaults, options),
activeClass = settings.classPrefix + settings.activeClass;
$.extend(settings, {
activeClass: activeClass,
activeClassSelector: '.' + activeClass
});
settings.messages = messages;
// Expose public API.
$.extend($.fn.h5Validate, h5);
// Returning the jQuery object allows for method chaining.
return methods.bindDelegation.call(this, settings);
};
}(jQuery));