forked from Steve-Fenton/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.easyvalidation.js
67 lines (52 loc) · 1.53 KB
/
jquery.easyvalidation.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
(function($)
{
// This script was written by Steve Fenton
// http://www.stevefenton.co.uk/Content/Jquery-Easy-Validation/
// Feel free to use this jQuery Plugin
// Version: 3.0.1
// Contributions by:
var classModifier = "";
var initialClass = "";
var errorClass = "";
var errorMessage = "";
// Raw validate function, based on required fields
function Validate(form) {
var valid = true;
$("."+initialClass).each( function () {
$(this).removeClass(errorClass);
if ($(this).val().length == 0) {
$(this).addClass(errorClass);
valid = false;
}
});
if (!valid) {
alert(errorMessage);
}
return valid;
}
// Hijack the submit events in order to perform validation
$("form").bind("submit", function () { return Validate(this) });
$.fn.easyvalidation = function (settings) {
var config = {
classmodifier: "ev",
initialclass: "musthave",
errorclass: "validationerror",
message: "Please complete all required fields"
};
if (settings) {
$.extend(config, settings);
}
// The class to denote a field is required
initialClass = config.initialclass;
// The class to show a field is errored
errorClass = config.errorclass;
// The error message
errorMessage = config.message;
return this.each(function () {
classModifier = config.classmodifier;
opacityAmount = config.opacity;
// This is to ensure all fields have the correct class (they may or may not already have it)
$(this).removeClass(initialClass).addClass(initialClass);
});
};
})(jQuery);