forked from twbs/bootstrap-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormValidator.js
164 lines (160 loc) · 4.25 KB
/
FormValidator.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
function FormValidator(t, e) {
this.form = t,
this.errors = [],
this.validators = [],
$(this.form).on("submit", $.proxy(this, "onSubmit")),
this.summary = e && e.summary ? $(e.summary) : $(".errorSummary"),
this.summary.on("click", "a", $.proxy(this, "onErrorClick")),
this.originalTitle = document.title
}
FormValidator.entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
"`": "`",
"=": "="
},
FormValidator.escapeHtml = function(t) {
return String(t).replace(/[&<>"'`=\/]/g, function(t) {
return FormValidator.entityMap[t]
})
}
,
FormValidator.prototype.onErrorClick = function(t) {
t.preventDefault();
var e = t.target.href
, o = e.substring(e.indexOf("#"), e.length);
$(o).focus()
}
,
FormValidator.prototype.resetTitle = function() {
document.title = this.originalTitle
}
,
FormValidator.prototype.updateTitle = function() {
document.title = "(" + this.errors.length + " errors) - " + document.title
}
,
FormValidator.prototype.showSummary = function() {
this.summary.html(this.getSummaryHtml()),
this.summary.removeClass("hidden"),
this.summary.attr("aria-labelledby", "errorSummary-heading"),
this.summary.focus()
}
,
FormValidator.prototype.getSummaryHtml = function() {
var t = '<h2 class="h5 errorSummary-heading">There\'s a problem</h2>';
t += "<ul>";
for (var e = 0, o = this.errors.length; e < o; e++) {
var i = this.errors[e];
t += "<li>",
t += '<a href="#' + FormValidator.escapeHtml(i.fieldName) + '">',
t += FormValidator.escapeHtml(i.message),
t += "</a>",
t += "</li>"
}
return t += "</ul>"
}
,
FormValidator.prototype.hideSummary = function() {
this.summary.addClass("hidden"),
this.summary.removeAttr("aria-labelledby")
}
,
FormValidator.prototype.onSubmit = function(t) {
this.removeInlineErrors();
this.hideSummary();
this.resetTitle();
if (!this.validate()) {
t.preventDefault();
this.updateTitle();
this.showSummary();
this.showInlineErrors();
}
}
,
FormValidator.prototype.showInlineErrors = function() {
for (var t = 0, e = this.errors.length; t < e; t++)
this.showInlineError(this.errors[t])
}
,
FormValidator.prototype.showInlineError = function(t) {
var e = '<span class="field-error"> ' + FormValidator.escapeHtml(t.message) + "</span>"
, o = $("#" + t.fieldName)
, i = o.parents(".field")
, n = i.find("label")
, s = i.find("legend");
i.find(".field-error").remove(),
s.length ? (s.append(e),
i.attr("aria-invalid", "true")) : (i.append(e),
o.attr("aria-invalid", "true"))
}
,
FormValidator.prototype.removeInlineErrors = function() {
$(this.form).find(".field-error").remove(),
$(this.form).find("[aria-invalid]").attr("aria-invalid", "false")
}
,
FormValidator.prototype.addValidator = function(t, e) {
this.validators.push({
fieldName: t,
rules: e,
field: this.form.elements[t]
})
}
,
FormValidator.prototype.validate = function() {
this.errors = [];
var t, e, o = null;
for (t = 0; t < this.validators.length; t++)
for (o = this.validators[t],
e = 0; e < o.rules.length; e++)
if (!o.rules[e].method(o.field, o.rules[e].params)) {
this.errors.push({
fieldName: o.fieldName,
message: o.rules[e].message
});
break
}
return 0 === this.errors.length
}
var validator = new FormValidator(document.getElementById('registration'));
validator.addValidator("email", [
{
method: function (field) {
return field.value.length > 0;
},
message: "Enter your email address",
},
{
method: function (field) {
return field.value.indexOf("@") > -1;
},
message: "You need to enter the ‘at’ symbol in the email address",
},
]);
validator.addValidator("password", [
{
method: function (field) {
return field.value.length > 0;
},
message: "Enter your password",
},
{
method: function (field) {
return field.value.length > 8;
},
message: "Your password must contain at least 8 characters",
},
]);
validator.addValidator('personal-details', [
{
method: function (field) {
return field.checked === true;
},
message: "You must waive your rights.",
}
])