-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
90 lines (79 loc) · 2.1 KB
/
Copy pathvalidator.js
File metadata and controls
90 lines (79 loc) · 2.1 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
(function($) {
$.fn.formValidate = function(options) {
var opt =
{
ruleSet: null,
onPass: function() {
// default do nothing
},
onFail: function(errors) {
var errMsg = "";
for (var i = 0; i < errors.length; i++) {
errMsg += ("+ " + errors[i] + "\n")
}
alert(errMsg);
}
};
$.extend(opt, options);
var results = {pass: true, errors: []};
if (opt.ruleSet == null) {
return this;
}
this.each(function() {
if (this.tagName == 'FORM') {
var curForm = this;
$(ruleSet).each(function() {
var elems = $(curForm).find(this.selector);
var displayName = this.displayName;
$(this.rules).each(function() {
var val = elems.val();
if (elems.is(":radio") || elems.is(":checkbox")) {
val = this.filter(":checked").val();
}
if (this.type == 'maxlength') {
if ($.trim(val).length > this.test) {
results.pass = false;
var msg = displayName + " is too long. It needs to be shorter than " + this.test + " characters";
}
}
else if (this.type == 'minlength') {
if ($.trim(val) != "" && $.trim(val).length < this.test) {
results.pass = false;
var msg = displayName + " is too short. It needs to be longer than " + this.test + " characters";
}
}
else if (this.type == 'required') {
if ($.trim(val) == "") {
results.pass = false;
var msg = displayName + " is required";
}
}
else if (this.type == 'knownGood') {
if ($.trim(val) != "" && this.test.test(val) == 0) {
results.pass = false;
var msg = displayName + " is invalid";
}
}
else if (this.type == 'custom') {
if (!this.test()) {
results.pass = false;
var msg = displayName + " failed validation";
}
}
if (this.errorMsg != undefined) {
msg = this.errorMsg;
}
results.errors.push(msg);
});
});
}
});
if (!results.pass) {
opt.onFail(results.errors);
}
else {
opt.onPass();
}
return this;
}
}(jQuery))