-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbvalidator.jquery.js
More file actions
559 lines (463 loc) · 14.8 KB
/
bvalidator.jquery.js
File metadata and controls
559 lines (463 loc) · 14.8 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
url: https://github.com/jBenes/jQuery-bValidator
*/
(function($) {
$.bValidator = {
defaults: {},
validations: {},
transformations: {},
//settings: {},
/**
* Bings input and form actions, initialises input values, stores settings into form node
*
* @param form element
* @param json settings
* @return void
*/
init: function(form, settings) {
var plugin = this;
// clear plugin bindings and data in order not to have double bindings
plugin.destruct(form);
// store settings to form node
form.data('bValidator', settings);
$('.' + settings.errorMessageClass, form).hide();
// choose elements which should be validated
var inputs = $('input[data-bvString], input[data-bvStrict], select[data-bvStrict], textarea[data-bvString], textarea[data-bvStrict]', form);
inputs.each(function (){
var input = $(this);
// bind focus in function
input.on('focusin.bValidator', function() {
var elem = $(this);
var form = elem.parents('form');
var settings = form.data('bValidator');
plugin.focus(elem, settings);
settings.onFocusIn.call( this, form, elem );
});
// choose event for binding validation action - change for checkboxes and selects. Other inputs focusout
var method = 'focusout';
if(input.is('select, input[type="checkbox"], input[type="radio"]')) {
method = 'change';
}
// bind validation event
input.on(method+'.bValidator', function() {
var elem = $(this);
var form = elem.parents('form');
plugin.validate(elem);
settings.onFocusOut.call( this, form, elem );
});
if(settings.onKeyUpValidate) {
input.on('keyup.bValidator', function() {
var elem = $(this);
var form = elem.parents('form');
plugin.validate(elem);
settings.onKeyUp.call( this, form, elem );
});
}
// initialize elements - apply transformations, pendings
plugin.initInput(input);
});
//plugin.settings: $.extend({}, defaults, options);
// bind before submit validations
form.on('submit.bValidator', function(e) {
// obtain settings from form node
var settings = $(this).data('bValidator');
// assume that form is valid
var bValid = true;
// choose elements which should be validated
var inputs = $('input[data-bvString], input[data-bvStrict], select[data-bvStrict], textarea[data-bvString], textarea[data-bvStrict]', $(this));
// validate all inputs
inputs.each(function() {
// if input is invalid, remember it for breaking submitting form later
if(!plugin.validate($(this))) bValid = false;
});
// if form is invalid
if(!bValid) {
// call submitFail callback
settings.onSubmitFail.call( this, this, e );
// and forbit submitting form
e.preventDefault();
/// if form is valid
} else {
// for each input apply focus action -> pendigs are stripped
inputs.each(function() {
plugin.focus($(this), settings);
});
// and call beforeSubmit callback
settings.beforeSubmit.call( this, this, e );
}
});
},
/**
* Parses input value, gets input config based on attributes
*
* @param input element
* @return input config
*/
getConfig: function(elem) {
var val = elem.val();
conf = {};
// switch value - erase inputs with this value. Fill value of empty inputs on focus out
conf['switchVal'] = (elem.attr('data-bvSwitch') == undefined ? '' : elem.attr('data-bvSwitch'));
// prepended value
conf['prepend'] = (elem.attr('data-bvPrepend') == undefined ? '' : elem.attr('data-bvPrepend'));
// key string
conf['string'] = (elem.attr('data-bvString') == undefined ? '.*' : elem.attr('data-bvString'));
// appended value
conf['append'] = (elem.attr('data-bvAppend') == undefined ? '' : elem.attr('data-bvAppend'));
// string transformation
conf['transform'] = (elem.attr('data-bvTransform') == undefined ? '' : elem.attr('data-bvTransform'));
// pattern for regexp
var pattern = '^.*('+conf['append']+')$';
var re = new RegExp(pattern, 'i');
var mand = '?';
if(val.match(re) != null && val.match(re)[1] != '') mand = '';
var pattern = '^('+conf['prepend']+')?'+'('+conf['string']+')('+conf['append']+')'+mand+'$';
var re = new RegExp(pattern, 'i');
// new string value
conf['newString'] = val.match(re)[2];
if(conf['switchVal'] == conf['newString']) conf['newString'] = '';
// string which fill for empty inputs
conf['empty'] = (elem.attr('data-bvEmpty') == undefined ? '' : elem.attr('data-bvEmpty'));
// rules for real validation
conf['strict'] = (elem.attr('data-bvStrict') == undefined ? '' : elem.attr('data-bvStrict'));
return conf;
},
/**
* Initialise input value - applies transformations, switchval, default val, pendings
*
* @param input element
* @return input config
*/
initInput: function(elem) {
// get input clean value, input config
conf = this.getConfig(elem);
// fix prepend escape chars
conf['prepend'] = conf['prepend'].replace(/\\|\?/g,'');
// fix append escape chars
conf['append'] = conf['append'].replace(/\\|\?/g,'');
if(elem.is('[type="file"]')) {
return conf;
}
// if element value is empty
if(!elem.val() || elem.val() == conf['empty'] || conf['newString'] == '') {
// and if both switchval and empty val are empty, add pendings
if(conf['empty'] == '' && conf['switchVal'] == '') elem.val(conf['prepend']+conf['append']);
// else if switchval is empty, apply Empty value
else if(conf['switchVal'] == '') elem.val(conf['empty']);
// otherwise add switchval value
else elem.val(conf['switchVal']);
//elem.addClass('grey');
}
// if value is not empty
else {
// apply transformation
if(conf['transform']) conf['newString'] = this.transform(conf['newString'], conf['transform']);
// and set value with pendings
elem.val(conf['prepend']+conf['newString']+conf['append']);
//elem.removeClass('grey');
}
return conf;
},
focus: function(elem, settings) {
conf = this.getConfig(elem);
if(!elem.is('[type="file"]')) {
if(elem.val() == conf['switchVal']) elem.val('');
else elem.val($.trim(conf['newString']));
}
//elem.removeClass('grey');
// hide error after focusing element
if(settings.onFocusHideError) {
this.clean(elem, settings);
}
},
isValid: function(value, rule, elem) {
if(typeof value === 'undefined') {
var conf = this.initInput(elem);
var value = conf['newString'];
}
if(typeof rule === 'undefined') {
var conf = this.initInput(elem);
var rule = conf['strict'];
}
var plugin = this;
var result = false;
var ruleEscaped = rule.replace(/({{((?!}}).)*}})/g, '{{NOPARSE}}');
var ruleReplacements = rule.match(/({{((?!}}).)*}})/g);
var rulesOr = ruleEscaped.split('|');
//rulesAnd.each();
$.each(rulesOr, function(indexOr, ruleOr) {
var rulesAnd = ruleOr.split('&');
var resultAnd = true;
$.each(rulesAnd, function(indexAnd, ruleAnd) {
var args = ruleAnd.split(':');
for (var i = 0; i < args.length; i++) {
if(args[i].match(/\{\{NOPARSE\}\}/) !== null) {
var replacement = ruleReplacements.shift();
args[i] = args[i].replace('{{NOPARSE}}', replacement.slice(2, -2));
}
};
if(!plugin.validations[args[0]].func(value, args, elem)) {
resultAnd = false;
return;
}
});
if(resultAnd == true) {
result = true;
return;
}
});
return result;
},
validate: function(elem) {
var form = elem.parents('form');
var settings = form.data('bValidator');
this.clean(elem, settings);
conf = this.initInput(elem);
if(this.isValid(conf['newString'], conf['strict'], elem)) return this.valid(form, settings, elem);
else return this.invalid(form, settings, elem);
},
validation: function() {
this.validations[arguments[0]] = {
func: arguments[1],
};
return this;
},
transform: function(value, method) {
return this.transformations[method].func(value);
},
transformation: function() {
this.transformations[arguments[0]] = {
func: arguments[1],
};
return this;
},
clean: function(elem, settings) {
if(settings.domType == 'direct') {
elem.removeClass(settings.errorClass)
.removeClass(settings.validClass);
elem.parents('form')
.find('.error-'+elem.attr('name'))
.removeClass(settings.errorClass)
.removeClass(settings.validClass)
.hide();
elem.parents('form')
.find('label[for="'+elem.attr('name')+'"]')
.removeClass(settings.errorClass)
.removeClass(settings.validClass);
} else {
elem.parents('.' + settings.rowClass)
.removeClass(settings.errorClass)
.removeClass(settings.validClass)
.find('.' + settings.errorMessageClass)
.hide();
}
},
valid: function(form, settings, elem) {
if(settings.domType == 'direct') {
elem.addClass(settings.validClass);
form.find('.error-'+elem.attr('name'))
.addClass(settings.validClass)
.hide();
form.find('label[for="'+elem.attr('name')+'"]')
.addClass(settings.validClass);
} else {
elem.parents('.' + settings.rowClass)
.addClass(settings.validClass);
elem.parents('.' + settings.rowClass)
.find('.' + settings.errorMessageClass)
.hide();
}
settings.onValid.call( this, form, elem );
return true;
},
invalid: function(form, settings, elem) {
if(settings.domType == 'direct') {
elem.addClass(settings.errorClass);
form.find('.error-'+elem.attr('name'))
.addClass(settings.errorClass)
.show();
form.find('label[for="'+elem.attr('name')+'"]')
.addClass(settings.errorClass);
} else {
elem.parents('.' + settings.rowClass)
.addClass(settings.errorClass);
elem.parents('.' + settings.rowClass)
.find('.' + settings.errorMessageClass)
.show();
}
settings.onInvalid.call( this, form, elem );
return false;
},
destruct: function(form) {
form.unbind('.bValidator');
var inputs = $('input[data-bvString], input[data-bvStrict], textarea[data-bvString], textarea[data-bvStrict]', form);
inputs.unbind('.bValidator');
form.removeData('bValidator');
}
}
$.fn.bValidator = function(options, args) {
if(typeof options == 'string' && options == 'isValid') {
var valid = true;
this.each(function() {
if(typeof args === 'undefined') {
args = Array();
}
if(!$.bValidator.isValid(undefined, args[0], $(this))) {
valid = false;
return;
}
});
return valid;
}
var settings = $.extend({
errorClass: 'error',
errorMessageClass: 'error-message',
validClass: 'valid',
rowClass: 'row',
onFocusHideError: false, // TODO: little overhead, lookup for settings after each validation
onKeyUpValidate: false,
domType: 'row', // other options: 'direct'
onValid: function() {},
onInvalid: function() {},
beforeSubmit: function() {},
onSubmitFail: function() {},
onFocusIn: function() {},
onFocusOut: function() {},
onKeyUp: function() {}
}, options );
return this.each(function() {
if(typeof options == 'string') {
switch(options) {
case 'validate':
return $.bValidator.validate($(this));
break;
case 'destruct':
return $.bValidator.destruct($(this));
break;
}
} else {
$this = $(this);
//if($this.is('form')) $.bValidator.init($this, settings);
//else $.bValidator.init($this.find('form'), settings);
if(!$this.is('form')) $this = $this.find('form');
return $this.each(function() {
$.bValidator.init($(this), settings);
});
}
//if (undefined == $(this).data('bValidator')) {
//var plugin = new $.bValidator.init();
//$(this).data('bValidator', plugin);
//}
});
}
})(jQuery);
jQuery.bValidator
.validation('number', function(value, args) {
valid = (value.match(/^[0-9]+$/) != null);
if(!valid) return false;
if(typeof args[1] != 'undefined') {
intVal = parseInt(value);
if(args[1] != '') {
if(intVal < args[1]) return false;
}
if(typeof args[2] != 'undefined') {
if(args[2] != '') {
if(intVal > args[2]) return false;
}
}
}
return true;
})
.validation('empty', function(value) {
return (value == '');
})
.validation('if', function(value, args, elem) {
// arg[0] = rule name -> if
// arg[1] = name of other input
// arg[2] = name of other input rule
// arg[3] = name of current input rule
var input = elem.parents('form').find('[name="'+args[1]+'"]');
if(input.bValidator('isValid', Array(args[2]))) {
return (elem.bValidator('isValid', Array(args[3])));
} else return true;
})
.validation('ifv', function(value, args, elem) {
// arg[0] = rule name -> ifv
// arg[1] = name of other input
// arg[2] = value of other input rule
// arg[3] = name of current input rule
if(elem.parents('form').find('[name="'+args[1]+'"]').val() == args[2]) {
return (elem.bValidator('isValid', Array(args[3])));
} else return true;
})
.validation('same', function(value, args) {
return (value == $('form [name="'+args[1]+'"]').val());
})
.validation('reg', function(value, args) {
return (value.match(args[1]) != null);
})
.validation('notEmpty', function(value) {
return (value.match(/^.+$/) != null);
})
.validation('string', function(value) {
return (value.match(/^[a-zA-Z]*$/) != null);
})
.validation('email', function(value) {
return (value.match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/) != null && value.length != 0);
})
.validation('zip', function(value) {
return (value.match(/^[0-9]{5}$/) != null);
})
.validation('city', function(value) {
return (value.match(/^.{2,}$/) != null);
})
.validation('date', function(value, args) {
console.log(args[1]);
var mask = regex = args[1];
var matches = day = month = year = null;
regex = regex.replace('mm', '[01][0-9]');
regex = regex.replace('m', '[1]?[0-9]');
regex = regex.replace('dd', '[0123][0-9]');
regex = regex.replace('d', '[0123]?[0-9]');
regex = regex.replace('yyyy', '[0-9]{4}');
regex = regex.replace('yy', '[0-9]{2}');
var pattern = '^' + regex + '$';
var re = new RegExp(pattern, '');
return (!!value.match(re));
})
.validation('nin', function(value) {
return (value.match(/^[0-9]{6}\/[0-9]{3,4}$/) != null);
})
.validation('false', function(value) {
return false;
})
.validation('true', function(value) {
return true;
})
.validation('checked', function(value, args, elem) {
return $(elem).prop('checked');
})
.validation('phone', function(value) {
var same = 0;
var inc = 0;
var i;
var pnumber = value;
if(!pnumber.match(/^[0-9]{9}$/)) {
return false;
}
for(i = 4;i < 9;i++) {
if(i != 3 && pnumber.charAt(i) == pnumber.charAt(i - 1)) {
same++;
}
if(parseInt(pnumber.charAt(i)) == (parseInt(pnumber.charAt(i - 1)) + 1) || (pnumber.charAt(i) == '0' && pnumber.charAt(i - 1) == '9')) {
inc++;
}
}
if(same >= 4 || inc >= 5) {
return false;
}
return true;
})
.transformation('noSpaces', function(value) {
return value.replace(/ /g,'');
});