-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.customValidationMsgs.js
More file actions
125 lines (97 loc) · 4.22 KB
/
jquery.customValidationMsgs.js
File metadata and controls
125 lines (97 loc) · 4.22 KB
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
/*
Project Name: Custom HTML5 Validation Messages
URL:
Author: Kevin O'Brien
Company: Clockwork Acive Media Systems
Company Site: clockwork.net
License: MIT
Copyright (C) 2013 Clockwork Active Media Systems
Version: 1.0
**************************************/
(function ($) {
"use strict";
// These locally scoped variables help minification by aliasing strings
var data_key = 'customValidationMsgs-options', // Namespace plugin data
event_suffix = '.customValidationMsgs', // Namspace events
// Custom events
evt_component_initialized = 'component_initialized' + event_suffix, // Cusotm event when plugin is done initializing
evt_request_validation = 'request_validation' + event_suffix,
// Global variables shared across all instances
useragent = navigator.userAgent;
// Scope Methods
var methods = {
// Ran when plugin is initialized for setup
init : function (settings) {
var $el = $(this),
// Options added here are defaults, if different values are passed when initiallized, these will be overridden
options = $.extend({
validation_types : ['valueMissing', 'typeMismatch', 'patternMismatch', 'tooLong', 'rangeUnderflow', 'rangeOverflow', 'stepMismatch', 'valid'],
type : this.nodeName.toLowerCase()
}, settings);
// As of Opera v11.6 @font-face makes validation message dissapear
if(useragent.indexOf('Opera') != -1) { $el.addClass('opera'); }
$el
// Store data on the element for use later
.data(data_key, options)
// Adds listeners to form
.customValidationMsgs('activate_listeners')
// Triggers custom validation event on all supported elements in form
.customValidationMsgs('validate_form')
// Broadcasts component initiallized event
.trigger(evt_component_initialized, options);
return this;
},
activate_listeners: function(){
var $el = $(this),
options = $el.data(data_key);
$el
.on('input' + event_suffix, 'input, select, textarea', methods._handle_validation_request)
.on('change' + event_suffix, 'input, select, textarea', methods._handle_validation_request)
.on(evt_request_validation, 'input, select, textarea', methods._handle_validation_request)
return this;
},
validate_form: function(){
$(this).find('input, select, textarea').each(function(){
$(this).trigger(evt_request_validation)
})
},
_handle_validation_request: function(evt){
var $el = $(evt.delegateTarget),
options = $el.data(data_key),
$input = $(this);
if(! this.validity) return;
// Defines a new custom validation type, matches one field to another (like confirm password)
//
// example: <input type="text" name="pass" id="pass" data-validateMatch="true" data-matchingValues_id="confirm_pass" required />
// <input type="text" name="confirm_pass" id="confirm_pass" data-validateMatch="true" data-matchingValues_id="pass" required />
if($input.attr('data-validateMatch')) {
if($('#'+input.attr('data-matchingValues_id')).val() != $input.val()) {
this.setCustomValidity(this.data('matchingValues'));
return this;
}else { this.setCustomValidity(null) }
}
// Converts supported validation messages to a custom validation message if one is available in the data attribute
for(var i = 0; i < options.validation_types.length; i++) {
var msg = $input.attr( 'data-' + options.validation_types[i] );
if(this.validity[options.validation_types[i]] && msg) {
this.setCustomValidity(msg);
break;
}else { this.setCustomValidity("") }
}
return this;
}
};
$.fn.customValidationMsgs = function (method) {
/**
* Used to prevent use of functions prefixed with an underscore
* if a method is provided call that, otherwise treat it as an initiation
* Do not edit below this
**/
var args = arguments;
if (methods[method] && method.charAt(0) !== '_') {
return $(this).map(function(i, val) { return methods[method].apply(this, Array.prototype.slice.call(args, 1)); });
} else if (typeof method === 'object' || !method) {
return $(this).map(function(i, val) { return methods.init.apply(this, args); });
}
};
}(jQuery));