Remoto - VFS: validator.js Source File
Remoto - VFS
validator.js
Go to the documentation of this file.
1 
2 define( [
3  ],
4  function()
5  {
6  'use strict';
7 
16  var validator = {
17 /*
18  "destDir": function(value,options)
19  {
20  var r = basicOptionValidate(value,options);
21 
22  return r;
23  },
24 
25  "destFile": function(value,options)
26  {
27  },
28 */
29 
42  "email": function(value,options)
43  {
44  var r = basicOptionValidate(value,options);
45 
46  if (r===true)
47  {
48  //var rx = /^([^@]+@[^@]+\.[\w]{2,}\s*,?\s*)+$/; //an email address optionally followed by other comma-separated email addresses.
49  var rx = /^[^@]+@[^@]+\.[a-zA-Z.]{2,}$/; //non-'@', '@', non-'@', '.', alpha or '.'
50 
51  if (!value.length || value.match(rx))
52  return true;
53  else
54  return "Invalid Email Address.";
55  }
56 
57  return r;
58  },
59 
72  "phone": function(value,options)
73  {
74  var r = basicOptionValidate(value,options);
75 
76  if (r===true)
77  {
78  var rx = /^([0-9\‍(\‍)\/\+ \-\+\.]*)$/; //primitive, but should work for now
79 
80  if (!value.length || value.match(rx))
81  return true;
82  else
83  return "Invalid Phone Number.";
84  }
85 
86  return r;
87  },
88 /*
89  "frameList": function(value,options)
90  {
91  var r = basicOptionValidate(value,options);
92 
93  if (r===true)
94  {
95  var stepRX = /^-?\d+--?\d+x\d+$/;
96  var rangeRX = /^-?\d+--?\d+$/;
97  var itemRX = /^-?\d+$/;
98  var parts = value.split(",");
99 
100  if (value != '')
101  { for(var i=0;i<parts.length;i++)
102  if (!(parts[i].match(stepRX) || parts[i].match(rangeRX) || parts[i].match(itemRX)))
103  return "Invalid frame list. (ie: 1-50,60,100-200x19)";
104  }
105 
106  return true;
107  }
108 
109  return r;
110  },
111 */
112 
125  "number": function(value,options)
126  {
127  if (isNaN(value))
128  return "Value cannot be parsed as a number.";
129 
130  var v = parseFloat(value);
131 
132  if ("min" in options)
133  {
134  if (v < parseFloat(options.min))
135  return "Value is below minimum of "+options.min;
136  }
137 
138  if ("max" in options)
139  {
140  if (v > parseFloat(options.max))
141  return "Value is above maximum of "+options.max;
142  }
143 
144  /* if (("round" in options) && (options.round !== null))
145  {
146  var p = parseInt(options.round) || 0;
147  var v2 = parseFloat(parseFloat(v).toFixed(p));
148  if (v !== v2)
149  return "Value has too many decimal places";
150  }
151  */
152  var r = basicOptionValidate(value,options);
153 
154  return r;
155  },
156 /*
157  "sourceFile": function(value,options)
158  {
159  },
160 */
161 
174  "text": function(value,options)
175  {
176  return basicOptionValidate(value,options);
177  },
178 
191  "time": function(value,options)
192  {
193  var r = basicOptionValidate(value,options);
194 
195  if (r===true)
196  {
197  var rx = /^\d\d:\d\d(:\d\d)?$/;
198 
199  if (value.match(rx))
200  return true;
201  else
202  return "Invalid Time.";
203  }
204 
205  return r;
206  },
207 
220  "url": function(value,options)
221  {
222  var r = basicOptionValidate(value,options);
223 
224  if (r===true)
225  {
226  //https://gist.github.com/HenkPoley/8899766
227  var rx = new RegExp(
228  "^" +
229  // protocol identifier
230  "(?:(?:https?|ftp)://)" +
231  // user:pass authentication
232  "(?:\\S+(?::\\S*)?@)?" +
233  "(?:" +
234  // IP address exclusion
235  // private & local networks
236  "(?!10(?:\\.\\d{1,3}){3})" +
237  "(?!127(?:\\.\\d{1,3}){3})" +
238  "(?!169\\.254(?:\\.\\d{1,3}){2})" +
239  "(?!192\\.168(?:\\.\\d{1,3}){2})" +
240  "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
241  // IP address dotted notation octets
242  // excludes loopback network 0.0.0.0
243  // excludes reserved space >= 224.0.0.0
244  // excludes network & broacast addresses
245  // (first & last IP address of each class)
246  "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
247  "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
248  "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
249  "|" +
250  // IPv6 RegEx - http://stackoverflow.com/a/17871737/273668
251  "\\[(" +
252  "([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|" + // 1:2:3:4:5:6:7:8
253  "([0-9a-fA-F]{1,4}:){1,7}:|" + // 1:: 1:2:3:4:5:6:7::
254  "([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|" + // 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
255  "([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|" + // 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
256  "([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|" + // 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
257  "([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|" + // 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
258  "([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|" + // 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
259  "[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|" + // 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
260  ":((:[0-9a-fA-F]{1,4}){1,7}|:)|" + // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
261  "fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|" + // fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index)
262  "::(ffff(:0{1,4}){0,1}:){0,1}" +
263  "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}" +
264  "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|" + // ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
265  "([0-9a-fA-F]{1,4}:){1,4}:" +
266  "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}" +
267  "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])" + // 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)
268  ")\\]" +
269  "|" +
270  "localhost" +
271  "|" +
272  // host name
273  "(?:xn--[a-z0-9\\-]{1,59}|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?){0,62}[a-z\\u00a1-\\uffff0-9]{1,63}))" +
274  // domain name
275  "(?:\\.(?:xn--[a-z0-9\\-]{1,59}|(?:[a-z\\u00a1-\\uffff0-9]+-?){0,62}[a-z\\u00a1-\\uffff0-9]{1,63}))*" +
276  // TLD identifier
277  "(?:\\.(?:xn--[a-z0-9\\-]{1,59}|(?:[a-z\\u00a1-\\uffff]{2,63})))" +
278  ")" +
279  // port number
280  "(?::\\d{2,5})?" +
281  // resource path
282  "(?:/[^\\s]*)?" +
283  "$", "i"
284  );
285 
286  if (value.match(rx))
287  return true;
288  else
289  return "Invalid URL.";
290  }
291 
292  return r;
293  },
294  };
295 
315  function basicOptionValidate(value,options)
316  {
317  if ("allowEmpty" in options)
318  {
319  var r = false;
320  if (value!==null && value!==undefined)
321  {
322  var e = options.allowEmpty === true || options.allowEmpty === "true";
323  r = (e || (""+value).length > 0); //either it's allowed to be empty or it's not actually empty
324  }
325 
326  if (!r) return "Field is not allowed to be empty.";
327  }
328 
329  return true;
330  }
331 
332  return validator;
333  }
334 );
335 
basicOptionValidate(value, options)
setter value
a setter DOCME
setter validator
Set this widget's validator function.