-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.html5validator.js
209 lines (178 loc) · 7.29 KB
/
jquery.html5validator.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
/*!
* HTML5validator
*/
;(function ( $, window, document, undefined ) {
// Create the defaults once
var pluginName = 'html5validator',
defaults = {
disableBrowserValidation: true,
disableAutoValidation: false,
messages: {
required: 'This field is required',
requiredCheckbox: 'This field is required',
minlength: 'The minimum length of this field is not met',
maxlength: 'The content of this field is too long',
email: 'This is not an email address'
},
customValidators: {},
fieldParentSelector: '.entry'
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options );
if ( options && options.messages ) {
this.options.messages = $.extend( {}, defaults.messages, options.messages );
}
this._defaults = defaults;
this._name = pluginName;
this.isValid = false;
this.validationErrors = [];
this.errorElements = [];
this.validators = $.extend( {}, this.validators, this.options.customValidators );
this.init();
}
Plugin.prototype.init = function () {
// Browser validation
if ( this.options.disableBrowserValidation ) {
this.element.noValidate = true;
}
// Bind submit event
$(this.element).submit($.proxy( this.onFormSubmit, this ));
// $('button').click($.proxy( this.onFormSubmit, this ));
};
Plugin.prototype.onFormSubmit = function() {
this.isValid = false;
this.visibleErrors = [];
this.validationErrors = [];
this.hideErrors();
// default validation
if ( !this.options.disableAutoValidation ) {
this.validate( 'required', $('input[type!=checkbox][required], textarea[required]', this.element) );
this.validate( 'requiredCheckbox', $('input[type=checkbox][required]', this.element) );
this.validate( 'minlength', $('input[data-minlength]', this.element) );
this.validate( 'maxlength', $('input[data-maxlength]', this.element) );
this.validate( 'email', $('input[type=email]', this.element) );
}
// validation rules set in the config
if ( this.options.fields ) {
for ( field in this.options.fields ) {
// check if the field exists
var fieldEl = $('[name=' + field + ']', this.element);
if ( fieldEl ) {
// extract validator rules
var rules = this.options.fields[field].split('|');
for ( i in rules ) {
this.validate(rules[i], fieldEl);
}
}
}
}
if ( this.validationErrors.length > 0) {
$(this.validationErrors).each( $.proxy( this.showError, this ) );
$(this.element).trigger('failed');
this.validationErrors[0].element.focus();
return false;
}
if ( typeof( this.options.success ) == 'function' ) {
return this.options.success();
}
return true;
};
Plugin.prototype.validate = function( rule, elements ) {
if (elements.length == 0 ) return false;
validatorRule = rule.split(':')[0];
// check if the validator exists
if ( this.validators[rule] ) {
elements.each( $.proxy( function(index, el) {
if ( !this.validators[validatorRule]( el, rule.split(':') ) ) {
this.validationErrors.push({
element: $(el),
type: validatorRule
});
}
}, this ) );
} else {
console.log('validator "' + rules[i] + '" not found!');
}
}
Plugin.prototype.validators = {
required: function( el ) {
if ( !$(el).val() ) {
return false;
}
return true;
},
requiredCheckbox: function( el ) {
if ( !el.checked ) {
return false;
}
return true;
},
minlength: function( el, arguments ) {
if ( arguments.length > 1 && arguments[1] <= $(el).val().length ) {
return true;
} else if ( $(el).data('minlength') <= $(el).val().length ) {
return true;
}
return false;
},
maxlength: function( el, arguments ) {
if ( arguments.length > 1 && arguments[1] > $(el).val().length ) {
return true;
} else if ( $(el).data('maxlength') > $(el).val().length ) {
return true;
}
return false;
},
email: function( el ) {
var regEx = /^([\w-\.\+]+@([\w-]+\.)+[\w-]{2,4})?$/;
return regEx.test( $(el).val() );
}
};
Plugin.prototype.showError = function(index, errorObject) {
// we only show the first error per element
if ( $.inArray( errorObject.element.attr('name'), this.visibleErrors ) != -1 ) {
return false;
}
this.visibleErrors.push(errorObject.element.attr('name'));
// default error message
var message = this.options.messages[errorObject.type];
// check for a custom error message on the element
if ( errorObject.element.data( errorObject.type + '-error' ) ) {
message = errorObject.element.data( errorObject.type + '-error' );
}
// check for a custom function to show the error
if ( typeof( this.options.showError ) == 'function' ) {
var errorElement = this.options.showError( errorObject.element, errorObject.type, message );
} else {
// fallback to the default action
errorObject.element.parents(this.options.fieldParentSelector).addClass('error');
if ( message ) {
var errorElement = $('<span />').addClass('error-inline').text(message);
errorObject.element.parents(this.options.fieldParentSelector).append( errorElement );
}
}
this.errorElements.push( errorElement );
};
Plugin.prototype.hideErrors = function() {
$(this.errorElements).each(function( index, el ) { $(el).remove(); });
this.errorElements = [];
// check for a custom function to show the error
if ( typeof( this.options.hideErrors ) == 'function' ) {
this.options.hideErrors();
} else {
$('.error', this.element).removeClass('error');
}
}
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
}
})( jQuery, window, document );