This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjuan-validez-basic-0.1.0.js
157 lines (131 loc) · 3.43 KB
/
juan-validez-basic-0.1.0.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
/*!
JuanValidez Library v0.1.0
https://github.com/jmhobbs/JuanValidez
Copyright 2011, John Hobbs
Licensed under the MIT license.
https://github.com/jmhobbs/JuanValidez/blob/master/LICENSE
*/
var JuanValidez = function () {
var _validators = {};
return {
/*
Get the names of all currently loaded validators.
\returns Array
*/
validators: function () {
var key,
keys = [];
for( key in _validators ) {
keys.push( key );
}
return keys;
},
/*
Register a validator.
\param name Name of the validator. Be unique but concise.
\param fn The validation function.
*/
addValidator: function ( name, fn ) {
_validators[name] = fn;
},
/*
Remove a registered validator.
\param name The name of the validator to remove.
*/
removeValidator: function ( name ) {
if( _validators[name] ) {
delete _validators[name];
}
},
/*
Execute a single validator. If the validator does not exist, the validation is false.
\param validator The name of the validator to run.
\param value The value to test.
\param args Arguments to pass to the validator, as an array. (optional)
\returns boolean True if the value validated. False if not.
*/
runValidator: function ( validator, value, args ) {
// args is optional
args = ( "undefined" == typeof args ) ? [] : args;
try {
if( ! _validators[validator] ) { throw new TypeError( "No such validator: " + validator ); }
return _validators[validator].apply( value, args );
}
catch ( error ) {
if( window.console && window.console.log ) { console.log( error ); }
return false;
}
},
/*
Validate a value given a JuanValidez formatted string.
Example:
// Validate the string "hello" against the "required" validator and the "length" validator (minimum length 4, maximum 10)
JuanValidez.validate( 'hello', 'required length:4,10' );
\param value The value to validate.
\param validators The string of validators to test against.
*/
validate: function ( value, validators ) {
// If there are no validation rules, it's valid.
if(
'undefined' == typeof validators ||
'' === validators.replace( /^\s*(\S*)\s*$/, '$1' )
) { return []; }
var vds = validators.split( ' ' ),
failed = [],
valid = true,
vd_valid,
args,
validator;
for( i in vds ) {
args = vds[i].split( ':' );
validator = args[0];
args = args.slice( 1 ).join( ':' ).split( ',' );
vd_valid = this.runValidator( validator, value, args );
if( ! vd_valid ) {
failed.push( validator );
valid = false;
}
}
return failed;
}
};
}();
JuanValidez.addValidator(
"required",
function () { return this.replace( /^\s*(\S*)\s*/, '$1' ) != ''; }
);
JuanValidez.addValidator(
"length",
function ( min, max ) {
if( "undefined" === typeof max ) {
return ( this.length >= min );
}
else {
return ( this.length >= min && this.length <= max );
}
}
);
/* Check if the string is a representation of a positive integer */
JuanValidez.addValidator(
"integer",
function () {
return /^[0-9]+$/.test( this );
}
);
/*
Email validation is a complex subject.
This is only a very simple test.
Implement your own if you really need something else.
*/
JuanValidez.addValidator(
"email",
function () {
return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test( this );
}
);
JuanValidez.addValidator(
"match",
function ( b ) {
return this == b;
}
);