OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
validator.js
1 /*
2  Validator v1.1.0
3  (c) Yair Even Or
4  https://github.com/yairEO/validator
5 
6  MIT-style license.
7 */
8 
9 var validator = (function($){
10  var message, tests, checkField, validate, mark, unmark, field, minmax, defaults,
11  validateWords, lengthRange, lengthLimit, pattern, alertTxt, data,
12  email_illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/,
13  email_filter = /^.+@.+\..{2,6}$/; // exmaple email "steve@s-i.photo"
14 
15  /* general text messages
16  */
17  message = {
18  invalid : 'invalid input',
19  checked : 'must be checked',
20  empty : 'please put something here',
21  min : 'input is too short',
22  max : 'input is too long',
23  number_min : 'too low',
24  number_max : 'too high',
25  url : 'invalid URL',
26  number : 'not a number',
27  email : 'email address is invalid',
28  email_repeat : 'emails do not match',
29  password_repeat : 'passwords do not match',
30  repeat : 'no match',
31  complete : 'input is not complete',
32  select : 'Please select an option'
33  };
34 
35  if(!window.console){
36  console={};
37  console.log=console.warn=function(){ return; }
38  }
39 
40  // defaults
41  defaults = {
42  alerts : true,
43  classes : {
44  item : 'item',
45  alert : 'alert',
46  bad : 'bad'
47  }
48  };
49 
50  /* Tests for each type of field (including Select element)
51  */
52  tests = {
53  sameAsPlaceholder : function(a){
54  return $.fn.placeholder && a.attr('placeholder') !== undefined && data.val == a.prop('placeholder');
55  },
56  hasValue : function(a){
57  if( !a ){
58  alertTxt = message.empty;
59  return false;
60  }
61  return true;
62  },
63  // 'linked' is a special test case for inputs which their values should be equal to each other (ex. confirm email or retype password)
64  linked : function(a,b){
65  if( b != a ){
66  // choose a specific message or a general one
67  alertTxt = message[data.type + '_repeat'] || message.no_match;
68  return false;
69  }
70  return true;
71  },
72  email : function(a){
73  if ( !email_filter.test( a ) || a.match( email_illegalChars ) ){
74  alertTxt = a ? message.email : message.empty;
75  return false;
76  }
77  return true;
78  },
79  // a "skip" will skip some of the tests (needed for keydown validation)
80  text : function(a, skip){
81  // make sure there are at least X number of words, each at least 2 chars long.
82  // for example 'john F kenedy' should be at least 2 words and will pass validation
83  if( validateWords ){
84  var words = a.split(' ');
85  // iterrate on all the words
86  var wordsLength = function(len){
87  for( var w = words.length; w--; )
88  if( words[w].length < len )
89  return false;
90  return true;
91  };
92 
93  if( words.length < validateWords || !wordsLength(2) ){
94  alertTxt = message.complete;
95  return false;
96  }
97  return true;
98  }
99  if( !skip && lengthRange && a.length < lengthRange[0] ){
100  alertTxt = message.min;
101  return false;
102  }
103 
104  // check if there is max length & field length is greater than the allowed
105  if( lengthRange && lengthRange[1] && a.length > lengthRange[1] ){
106  alertTxt = message.max;
107  return false;
108  }
109 
110  // check if the field's value should obey any length limits, and if so, make sure the length of the value is as specified
111  if( lengthLimit && lengthLimit.length ){
112  while( lengthLimit.length ){
113  if( lengthLimit.pop() == a.length ){
114  alertTxt = message.complete;
115  return false;
116  }
117  }
118  }
119 
120  if( pattern ){
121  var regex, jsRegex;
122  switch( pattern ){
123  case 'alphanumeric' :
124  regex = /^[a-zA-Z0-9]+$/i;
125  break;
126  case 'numeric' :
127  regex = /^[0-9]+$/i;
128  break;
129  case 'phone' :
130  regex = /^\+?([0-9]|[-|' '])+$/i;
131  break;
132  default :
133  regex = pattern;
134  }
135  try{
136  jsRegex = new RegExp(regex).test(a);
137  if( a && !jsRegex )
138  return false;
139  }
140  catch(err){
141  console.log(err, field, 'regex is invalid');
142  return false;
143  }
144  }
145 
146  return true;
147  },
148  number : function(a){
149  // if not not a number
150  if( isNaN(parseFloat(a)) && !isFinite(a) ){
151  alertTxt = message.number;
152  return false;
153  }
154  // not enough numbers
155  else if( lengthRange && a.length < lengthRange[0] ){
156  alertTxt = message.min;
157  return false;
158  }
159  // check if there is max length & field length is greater than the allowed
160  else if( lengthRange && lengthRange[1] && a.length > lengthRange[1] ){
161  alertTxt = message.max;
162  return false;
163  }
164  else if( minmax[0] && (a|0) < minmax[0] ){
165  alertTxt = message.number_min;
166  return false;
167  }
168  else if( minmax[1] && (a|0) > minmax[1] ){
169  alertTxt = message.number_max;
170  return false;
171  }
172  return true;
173  },
174  // Date is validated in European format (day,month,year)
175  date : function(a){
176  var day, A = a.split(/[-./]/g), i;
177  // if there is native HTML5 support:
178  if( field[0].valueAsNumber )
179  return true;
180 
181  for( i = A.length; i--; ){
182  if( isNaN(parseFloat(a)) && !isFinite(a) )
183  return false;
184  }
185  try{
186  day = new Date(A[2], A[1]-1, A[0]);
187  if( day.getMonth()+1 == A[1] && day.getDate() == A[0] )
188  return day;
189  return false;
190  }
191  catch(er){
192  console.log('date test: ', err);
193  return false;
194  }
195  },
196  url : function(a){
197  // minimalistic URL validation
198  function testUrl(url){
199  return /^(https?:\/\/)?([\w\d\-_]+\.+[A-Za-z]{2,})+\/?/.test( url );
200  }
201  if( !testUrl( a ) ){
202  alertTxt = a ? message.url : message.empty;
203  return false;
204  }
205  return true;
206  },
207  hidden : function(a){
208  if( lengthRange && a.length < lengthRange[0] ){
209  alertTxt = message.min;
210  return false;
211  }
212  if( pattern ){
213  var regex;
214  if( pattern == 'alphanumeric' ){
215  regex = /^[a-z0-9]+$/i;
216  if( !regex.test(a) ){
217  return false;
218  }
219  }
220  }
221  return true;
222  },
223  select : function(a){
224  if( !tests.hasValue(a) ){
225  alertTxt = message.select;
226  return false;
227  }
228  return true;
229  }
230  };
231 
232  /* marks invalid fields
233  */
234  mark = function( field, text ){
235  if( !text || !field || !field.length )
236  return false;
237 
238  // check if not already marked as a 'bad' record and add the 'alert' object.
239  // if already is marked as 'bad', then make sure the text is set again because i might change depending on validation
240  var item = field.closest('.' + defaults.classes.item),
241  warning;
242 
243  if( item.hasClass(defaults.classes.bad) ){
244  if( defaults.alerts )
245  item.find('.'+defaults.classes.alert).html(text);
246  }
247 
248 
249  else if( defaults.alerts ){
250  warning = $('<div class="'+ defaults.classes.alert +'">').html( text );
251  item.append( warning );
252  }
253 
254  item.removeClass(defaults.classes.bad);
255  // a delay so the "alert" could be transitioned via CSS
256  setTimeout(function(){
257  item.addClass(defaults.classes.bad);
258  }, 0);
259  };
260  /* un-marks invalid fields
261  */
262  unmark = function( field ){
263  if( !field || !field.length ){
264  console.warn('no "field" argument, null or DOM object not found');
265  return false;
266  }
267 
268  field.closest('.' + defaults.classes.item)
269  .removeClass(defaults.classes.bad)
270  .find('.'+ defaults.classes.alert).remove();
271  };
272 
273  function testByType(type, value){
274  if( type == 'tel' )
275  pattern = pattern || 'phone';
276 
277  if( !type || type == 'password' || type == 'tel' || type == 'search' || type == 'file' )
278  type = 'text';
279 
280 
281  return tests[type] ? tests[type](value, true) : true;
282  }
283 
284  function prepareFieldData(el){
285  field = $(el);
286 
287  field.data( 'valid', true ); // initialize validity of field
288  field.data( 'type', field.attr('type') ); // every field starts as 'valid=true' until proven otherwise
289  pattern = field.attr('pattern');
290  }
291 
292  /* Validations per-character keypress
293  */
294  function keypress(e){
295  prepareFieldData(this);
296  // String.fromCharCode(e.charCode)
297 
298  if( e.charCode ){
299  return testByType( this.type, this.value );
300  }
301  }
302 
303  /* Checks a single form field by it's type and specific (custom) attributes
304  */
305  function checkField(){
306  // skip testing fields whom their type is not HIDDEN but they are HIDDEN via CSS.
307  if( this.type !='hidden' && $(this).is(':hidden') )
308  return true;
309 
310  prepareFieldData(this);
311 
312  field.data( 'val', field[0].value.replace(/^\s+|\s+$/g, "") ); // cache the value of the field and trim it
313  data = field.data();
314 
315  // Check if there is a specific error message for that field, if not, use the default 'invalid' message
316  alertTxt = message[field.prop('name')] || message.invalid;
317 
318  // Special treatment
319  if( field[0].nodeName.toLowerCase() === "select" ){
320  data.type = 'select';
321  }
322  else if( field[0].nodeName.toLowerCase() === "textarea" ){
323  data.type = 'text';
324  }
325  /* Gather Custom data attributes for specific validation:
326  */
327  validateWords = data['validateWords'] || 0;
328  lengthRange = data['validateLengthRange'] ? (data['validateLengthRange']+'').split(',') : [1];
329  lengthLimit = data['validateLength'] ? (data['validateLength']+'').split(',') : false;
330  minmax = data['validateMinmax'] ? (data['validateMinmax']+'').split(',') : ''; // for type 'number', defines the minimum and/or maximum for the value as a number.
331 
332  data.valid = tests.hasValue(data.val);
333 
334  if( field.hasClass('optional') && !data.valid )
335  data.valid = true;
336 
337 
338  // for checkboxes
339  if( field[0].type === "checkbox" ){
340  data.valid = field[0].checked;
341  alertTxt = message.checked;
342  }
343 
344  // check if field has any value
345  else if( data.valid ){
346  /* Validate the field's value is different than the placeholder attribute (and attribute exists)
347  * this is needed when fixing the placeholders for older browsers which does not support them.
348  * in this case, make sure the "placeholder" jQuery plugin was even used before proceeding
349  */
350  if( tests.sameAsPlaceholder(field) ){
351  alertTxt = message.empty;
352  data.valid = false;
353  }
354 
355  // if this field is linked to another field (their values should be the same)
356  if( data.validateLinked ){
357  var linkedTo = data['validateLinked'].indexOf('#') == 0 ? $(data['validateLinked']) : $(':input[name=' + data['validateLinked'] + ']');
358  data.valid = tests.linked( data.val, linkedTo.val() );
359  }
360  /* validate by type of field. use 'attr()' is proffered to get the actual value and not what the browsers sees for unsupported types.
361  */
362  else if( data.valid || data.type == 'select' )
363  data.valid = testByType(data.type, data.val);
364 
365  }
366 
367  // mark / unmark the field, and set the general 'submit' flag accordingly
368  if( data.valid )
369  unmark( field );
370  else{
371  mark( field, alertTxt );
372  submit = false;
373  }
374 
375  return data.valid;
376  }
377 
378  /* vaildates all the REQUIRED fields prior to submiting the form
379  */
380  function checkAll( $form ){
381  $form = $($form);
382 
383  if( $form.length == 0 ){
384  console.warn('element not found');
385  return false;
386  }
387 
388  var that = this,
389  submit = true, // save the scope
390  // get all the input/textareas/select fields which are required or optional (meaning, they need validation only if they were filled)
391  fieldsToCheck = $form.find(':input').filter('[required=required], .required, .optional').not('[disabled=disabled]');
392 
393  fieldsToCheck.each(function(){
394  // use an AND operation, so if any of the fields returns 'false' then the submitted result will be also FALSE
395  submit = submit * checkField.apply(this);
396  });
397 
398  return !!submit; // casting the variable to make sure it's a boolean
399  }
400 
401  return {
402  defaults : defaults,
403  checkField : checkField,
404  keypress : keypress,
405  checkAll : checkAll,
406  mark : mark,
407  unmark : unmark,
408  message : message,
409  tests : tests
410  }
411 })(jQuery);