56

Validator

The Validator class allows to verify the validity of a value for a data type. NOTE: The Validator class is an independent code which can be used without Objective.js in any project in JavaScript .

  1. function Validator() {
  2. }
  3.  
  4. Validator.prototype = Object.create(Objective.prototype);
  5.  
  6. Object.defineProperty(Validator.prototype, 'constructor', { value: Validator, enumerable: false, writable: true });
  7.  
  8. Validator.validateColor = function(color) {
  9.     return typeof color === 'string' && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(color);
  10. };
  11.  
  12. Validator.normalizeColor = function(color) {
  13.     return color.length == 4 ? color.replace(/([0-9a-fA-F])/g, '$1$1').toLowerCase() : color.toLowerCase();
  14. };
  15.  
  16. Validator.easingOptions = ['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out' ];
  17.  
  18. Validator.validateEasing = function(easing) {
  19.     return typeof easing === 'string' && Validator.easingOptions.indexOf(easing) != -1;
  20. };
  21.  
  22. Validator.textAlignmentOptions = ['left', 'center', 'right' ];
  23.  
  24. Validator.validateTextAlignment = function(alignment) {
  25.     return typeof alignment === 'string' && Validator.textAlignmentOptions.indexOf(alignment) != -1;
  26. };
  27.  
  28. Validator.validateFileName = function(filename) {
  29.     return typeof filename === 'string' &&  /^[a-zA-Z]+[0-9a-zA-Z._-]*(.[0-9a-zA-Z]+)?$/.test(filename);
  30. };
  31.  
  32. Validator.validateCookieName = function(cookiename) {
  33.     return typeof cookiename === 'string' &&  /^[a-zA-Z]+[0-9a-zA-Z._-]*$/.test(cookiename);
  34. };
  35.  
  36. Validator.validateModelName = function(modelname) {
  37.     return Validator.validateCookieName(modelname);
  38. };
  39.  
  40. Validator.validateImageDataURL = function(dataurl) {
  41.     return typeof dataurl === 'string' && /^data:image\/[-.+0-9a-zA-Z]+;base64,/.test(dataurl);
  42. };
Test
  1. <?php head('javascript', '/objectivejs/Objective.js'); ?>
  2. <?php head('javascript', '/objectivejs/Validator.js'); ?>
  1. <script>
  1. console.log(Validator.validateColor('#123456'));    // true
  2. console.log(Validator.validateColor('#789ABC'));    // true
  3. console.log(Validator.validateColor('#ABCDEF'));    // true
  4. console.log(Validator.validateColor('#abcdef'));    // true
  5.  
  6. console.log(Validator.validateColor('#ABCDE'));     // false
  7. console.log(Validator.validateColor('abcdef'));     // false
  8.  
  9. console.log(Validator.validateColor(0));            // false
  10.  
  11. console.log(Validator.normalizeColor('#2aF'));      // #22aaff
  12. console.log(Validator.normalizeColor('#27aADb'));   // #27aadb
  13.  
  14. console.log(Validator.validateEasing('linear'));    // true
  15. console.log(Validator.validateEasing('tease'));     // false
  16.  
  17. console.log(Validator.validateTextAlignment('center'));     // true
  18. console.log(Validator.validateTextAlignment('middle'));     // false
  19.  
  20. console.log(Validator.validateFileName('foobar'));      // true
  21. console.log(Validator.validateFileName('foobar.txt'));  // true
  22. console.log(Validator.validateFileName('/foo/bar'));    // false
  23. console.log(Validator.validateFileName('../foobar'));   // false
  24.  
  25. console.log(Validator.cookieName(uniqid('cookie'));         // true
  26. console.log(Validator.cookieName(md5(uniqid('cookie')));    // true
  27. console.log(Validator.cookieName('cookie' . '_' . uniqid)); // true
  28. console.log(Validator.cookieName('cookie' . '=' . uniqid)); // false
  29.  
  30. console.log(Validator.modelName(uniqid('VideoModel'));      // true
  31. console.log(Validator.modelName(uniqid('_DataModel'));      // false
  1. </script>

Display the page generated by the file testValidator.phtml and check the trace in the console of the browser:

true
true
true
true
false
false
false
#22aaff
#27aadb
true
false
true
false
true
true
false
false
true
true
true
false
true
false
SEE ALSO

ColorInspector, ImageInspector, Model

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].