-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
44 lines (39 loc) · 1.03 KB
/
validate.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
// JavaScript Document
;$ = jQuery.noConflict();(function($){
$.fn.validate = function(options){
var self = this;
var settings = $.extend({
requiredClass : $('.required'),
scrollToErrors : false
}, options);
self.on('submit', function(e){
//Loop through all required fields, if value is '' add error class
settings.requiredClass.each(function(index, element){
var val = $.trim($(this).val());
if(val == '')
{
$(this).addClass('InputError');
}
});
if($('.InputError').length)
{
if(settings.scrollToErrors){
var pos = $('.InputError:first').offset();
$('html, body').animate({
scrollTop: pos.top - 100
}, 400);
}
return false;
}
});
//Key up function for input text fields
// *** Needs specification for if the field is a text field
settings.requiredClass.on('keyup', function(e){
var val = $.trim($(this).val());
if(val != '' && $(this).hasClass('InputError'))
{
$(this).removeClass('InputError');
}
});
}
}(jQuery));