-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
177 lines (146 loc) · 4.66 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
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
/*! jQuery Validation Plugin - v1.0.0 - 8/1/2017
* https://github.com/emanuelerangan
* Copyright (c) 2017 Emanuele Rangan; Licensed MIT */
var validateOptions = {
rules: {
min: {
phoneUS:true,
required:true
},
vkey: {
digits:true,
required: true
}
},
messages: {
global : {
required: "This field is required.",
digits: "Please enter only digits."
},
min: {
email:"This is not a real email address",
phoneUS:"Please specify a valid phone number",
digits:"Numbers only please"
}
},
}
var validateMethods = {
required: function(input) {
var val = input.val();
return val.trim() != '';
},
phoneUS: function(input) {
var val = input[0].value
return (val.length > 9 && /^(1-?)?(\([0-9]\d{2}\)|[0-9]\d{2})-?[0-9]\d{2}-?\d{4}$/.test(val));
},
digits: function(input) {
var val = input.val();
return /^\d+$/.test(val);
},
}
$.fn.validate = function( options ) {
var form = this;
var errorsObj = {};
var formValidationObj = {}
// create the global obj errorsObj, which contains all the possible error messages for each DOM field within the form
function buildValidation(options) {
var opts = typeof options != 'undefined' ? options : typeof validateOptions != 'undefined' ? validateOptions : null;
if (opts != null) {
$.each(opts.rules, function(key) {
var singleInputRules = this;
if ($('[name="' + key + '"]').length) {
$.each(singleInputRules, function(_ruleName) {
var ruleName = _ruleName,
msg;
if (typeof opts.messages.global != 'undefined' && opts.messages.global.hasOwnProperty(ruleName)) {
msg = opts.messages.global[ruleName]
}
else if (typeof opts.messages[key] != 'undefined' && opts.messages[key].hasOwnProperty(ruleName)) {
msg = opts.messages[key][ruleName]
}
errorsObj[key] = errorsObj[key] ? errorsObj[key] : {}
errorsObj[key][ruleName] = {'msg': msg}
formValidationObj[key] = true;
})
}
})
}
else {
console.warn('Please specify an options object! $(form).validate(options)')
}
}
function fullValidate() {
$.each(errorsObj,function(key) {
var field = $('[name="' + key + '"]')
var fieldName = field.attr('name');
var isValidInput = true;
$.each(this,function(ruleName) {
var nextField = field.next();
var isValidRule = validateMethods[ruleName](field);
isValidInput = (isValidInput && isValidRule)
// set boolean validation for each input
formValidationObj[fieldName] = isValidInput;
if (typeof validateMethods[ruleName] == 'function' && !isValidRule) {
if (!nextField.hasClass('error')) {
field.after('<span class="error" style="color:#ED4337">' + this.msg + '</span>')
}
else {
nextField.html(this.msg)
}
}
if (typeof validateMethods[ruleName] == 'function' && isValidInput) { // nextField.hasClass('error')
(nextField.hasClass('error')) ? nextField[0].innerHTML = '' : field.after('<span class="error" style="color:#ED4337"></span>')
}
})
})
}
function validateField(field) {
var fieldName = field.attr('name');
// there's a possible error for the input with name fieldName
if (typeof errorsObj[fieldName] != 'undefined' && errorsObj[fieldName]) {
var isValidInput = true;
$.each(errorsObj[fieldName],function(ruleName) {
var nextField = field.next();
var isValidRule = validateMethods[ruleName](field);
isValidInput = (isValidInput && isValidRule)
// set boolean validation for each input
formValidationObj[fieldName] = isValidInput;
if (typeof validateMethods[ruleName] == 'function' && !isValidRule) {
if (!nextField.hasClass('error')) {
field.after('<span class="error" style="color:#ED4337">' + this.msg + '</span>')
}
else {
nextField[0].innerHTML = this.msg
}
}
if (typeof validateMethods[ruleName] == 'function' && isValidInput) {
(nextField.hasClass('error')) ? nextField[0].innerHTML = '' : field.after('<span class="error" style="color:#ED4337"></span>')
}
})
}
}
function handleSubmit(form) {
errorsObj && fullValidate();
var isValidForm = true;
$.each(formValidationObj,function(key) {
!formValidationObj[key] && (isValidForm = false)
})
isValidForm && form.submit();
}
buildValidation(options);
form.on('submit', function(event) {
event.preventDefault();
handleSubmit(this);
})
form.find('input, textarea, select').each(function() {
console.log('type: ,', $(this).attr('type'))
if ($(this).attr('type') != 'hidden') {
$(this).on('propertychange change click keyup input paste keypress',function() {
validateField($(this))
})
}
})
};
$(document).ready(function() {
$('form').eq(0).validate()
})