This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
/
validators.js
executable file
·70 lines (53 loc) · 1.58 KB
/
validators.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
/**
* Validator functions.
*/
export function required(value, arg) {
if (!(typeof arg == 'boolean')) {
arg = true;
}
if (typeof value == 'boolean') {
return !arg || value;
}
return !arg || !((value === null) || (value.length === 0));
}
export function numeric(value) {
return /^[-+]?[0-9]+$/.test(value);
}
export function integer(value) {
return /^(?:[-+]?(?:0|[1-9][0-9]*))$/.test(value);
}
export function float(value) {
return /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/.test(value);
}
export function alpha(value) {
return /^([A-Z]+)?$/i.test(value);
}
export function alphanum(value) {
return /^([0-9A-Z]+)?$/i.test(value);
}
export function email(value) {
return /^([a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*)?$/i.test(value || 'a@a.aa');
}
export function url(value) {
return /^((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)?$/.test(value);
}
export function minlength(value, arg) {
return value && value.length && value.length >= +arg;
}
export function maxlength(value, arg) {
return value && value.length && value.length <= +arg;
}
export function length(value) {
return value && value.length == +arg;
}
export function min(value, arg) {
return value >= +arg;
}
export function max(value, arg) {
return value <= +arg;
}
export function pattern(value, arg) {
var match = arg.match(new RegExp('^/(.*?)/([gimy]*)$'));
var regex = new RegExp(match[1], match[2]);
return regex.test(value);
}