-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.validation.js
45 lines (41 loc) · 1.65 KB
/
jquery.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
// Simple client side form validation with HTML5 required attribute.
// By: Gary Von Schilling
// How to use:
// 1. Add "required" attribute to fields in a form
// 2. Call $('form').validate();
// 3. To change the message use $('form').validate({message: 'Your custom error message.'});
// For full featured form validation, try http://parsleyjs.org instead
(function($){
var defaults = {
message: 'Please fill in all required fields.',
feedbackClass: 'feedback'
};
$.fn.validate = function(options) {
options = $.extend(defaults, options||{});
return this.each(function() {
var $form = $(this);
$form.bind('submit', function (e) {
var valid = true;
$form.find('[required]').each(function(i, field) {
if (valid && !field.value) {
valid = false;
$(field).trigger('focus').fadeOut().fadeIn();
if (field.id) {
$form.find('label[for="' + field.id + '"]')
.fadeOut().fadeIn();
}
}
});
if (!valid) {
if (!$form.find('.' + options.feedbackClass).length) {
$form.prepend('<div class="' + options.feedbackClass + '"/>');
}
$form.find('.' + options.feedbackClass)
.html(options.message).fadeOut().fadeIn();
e.preventDefault(); return false;
}
});
});
};
})(jQuery);
// $('form:last').validate();