-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_validator.js
224 lines (188 loc) · 6.78 KB
/
form_validator.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// JQUERY PLUGIN FOR FORM VALIDATION
// Author: Waqas Ahmed
// Date: 4 August 2016
// First Letter Capital
//http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
String.prototype.wucword = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
//http://stackoverflow.com/questions/2855865/jquery-regex-validation-of-e-mail-address
String.prototype.isValidEmail = function () {
var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
return pattern.test(this);
}
//
String.prototype.isValidNumber = function () {
var pattern = /^\d+$/;
return pattern.test(this);
}
String.prototype.validateDate = function () {
return isValidDate(this);
}
// Validates that the input string is a valid date formatted as ("mm/dd/yyyy", "dd/mm/yyyy", "dd.mm.yyyy", "yyyy-mm-dd")
function isValidDate (dateString) {
var dateFormatReg, parts, month, day, year, separator;
if (portal_dateformat == undefined || portal_dateformat == null) {
portal_dateformat = "mm/dd/yyyy";
}
switch (portal_dateformat) {
case "YYYY-MM-DD":
case "yyyy-mm-dd":
dateFormatReg = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
separator = "-";
break;
case "DD/MM/YYYY":
case "dd/mm/yyyy":
dateFormatReg = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
separator = "/";
break;
case "DD.MM.YYYY":
case "dd.mm.yyyy":
dateFormatReg = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
separator = ".";
break;
default:
// MM/DD/YYYY
// mm/dd/yyyy
dateFormatReg = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
separator = "/";
break;
}
// First check for the pattern
if(!dateFormatReg.test(dateString))
return false;
// Parse the date parts to integers
switch (separator) {
case ".":
parts = dateString.split(".");
day = parseInt(parts[1], 10);
month = parseInt(parts[0], 10);
year = parseInt(parts[2], 10);
break;
case "-":
parts = dateString.split("-");
day = parseInt(parts[2], 10);
month = parseInt(parts[1], 10);
year = parseInt(parts[0], 10);
break;
default:
parts = dateString.split("/");
if (portal_dateformat === "DD/MM/YYYY"
|| portal_dateformat === "dd/mm/yyyy") {
day = parseInt(parts[0], 10);
month = parseInt(parts[1], 10);
year = parseInt(parts[2], 10);
} else {
day = parseInt(parts[1], 10);
month = parseInt(parts[0], 10);
year = parseInt(parts[2], 10);
}
break;
}
// Check the ranges of month and year
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
// Check the range of the day
return day > 0 && day <= monthLength[month - 1];
};
// Validates form fields
$.fn.wValidateForm = function () {
//var a = this.serializeArray();
// To validate all fields within any container
var a = $(this).find('input,textarea,select');
// Apply class to inputs
a.removeClass('bdr-red');
var inp = new Array();
var invalid_entry = new Array();
var correct_entry = new Array();
var alertMsg = '';
var password = null, confirm_password = null;
$.each(a, function () {
var e = $("[name='"+this.name+"']");
// Check added for serialize array and non serialize array
if (o instanceof HTMLElement) {
e = $(this);
}
// incase button
if ($(e).is('button')) {
return true;
}
var v = e.val();
// field is required & empty
if (e.hasClass("required")
&& $.trim(v) == "") {
inp.push(e);
}
// invalid input field check
// Email Check
if (e.hasClass("valid_email")
&& $.trim(v) != ""
&& !v.isValidEmail()) {
invalid_entry.push(e);
correct_entry.push('email');
}
// Number or Numeric check
if (e.hasClass("valid_number")
&& $.trim(v) != ""
&& !v.isValidNumber()) {
invalid_entry.push(e);
correct_entry.push('number');
}
// Date format check
if (e.hasClass("valid_date")
&& $.trim(v) != ""
&& !v.validateDate()) {
invalid_entry.push(e);
correct_entry.push('date');
}
if (this.name == "password") {
password = v;
}
if (this.name == "confirm_password") {
confirm_password = v;
}
});
// Required field check message
if (inp.length > 0) {
$.each(inp, function(i, e){
var eleName = e.prop('name');
var eleMsg = $.trim(eleName.wucword().replace(/[_\[\]\{\}]|temp/gi, '')) + " is required.\n";
// data-label exists
// to show custom message for specific field
if ($.trim($(e).data('label')) != "") {
eleMsg = $(e).data('label') + " is required.\n";
}
// Apply forder
e.addClass('bdr-red');
alertMsg += eleMsg;
});
alert(alertMsg);
return false;
}
// validate entered value message
if (invalid_entry.length > 0) {
$.each(invalid_entry, function(i, e){
var eleName = e.prop('name');
var eleMsg = "Invalid "+correct_entry[i]+" is entered.\n";
// Apply border
e.addClass('bdr-red');
alertMsg += eleMsg;
});
alert(alertMsg);
return false;
}
// Password & Confirm Password
if ( password != null
&& confirm_password != null
&& $.trim(password) != ""
&& $.trim(confirm_password) != ""
&& (password != confirm_password)) {
alert("Password & Confirm Password should match.");
return false;
}
return true;
}