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 .
- function Validator() {
- }
- Validator.prototype = Object.create(Objective.prototype);
- Object.defineProperty(Validator.prototype, 'constructor', { value: Validator, enumerable: false, writable: true });
- Validator.validateColor = function(color) {
- return typeof color === 'string' && /^#(?:[0-9a-fA-F]{3}){1,2}$/.test(color);
- };
- Validator.normalizeColor = function(color) {
- return color.length == 4 ? color.replace(/([0-9a-fA-F])/g, '$1$1').toLowerCase() : color.toLowerCase();
- };
- Validator.easingOptions = ['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out' ];
- Validator.validateEasing = function(easing) {
- return typeof easing === 'string' && Validator.easingOptions.indexOf(easing) != -1;
- };
- Validator.textAlignmentOptions = ['left', 'center', 'right' ];
- Validator.validateTextAlignment = function(alignment) {
- return typeof alignment === 'string' && Validator.textAlignmentOptions.indexOf(alignment) != -1;
- };
- Validator.validateFileName = function(filename) {
- return typeof filename === 'string' && /^[a-zA-Z]+[0-9a-zA-Z._-]*(.[0-9a-zA-Z]+)?$/.test(filename);
- };
- Validator.validateCookieName = function(cookiename) {
- return typeof cookiename === 'string' && /^[a-zA-Z]+[0-9a-zA-Z._-]*$/.test(cookiename);
- };
- Validator.validateModelName = function(modelname) {
- return Validator.validateCookieName(modelname);
- };
- Validator.validateImageDataURL = function(dataurl) {
- return typeof dataurl === 'string' && /^data:image\/[-.+0-9a-zA-Z]+;base64,/.test(dataurl);
- };
Test
- <?php head('javascript', '/objectivejs/Objective.js'); ?>
- <?php head('javascript', '/objectivejs/Validator.js'); ?>
- <script>
- console.log(Validator.validateColor('#123456')); // true
- console.log(Validator.validateColor('#789ABC')); // true
- console.log(Validator.validateColor('#ABCDEF')); // true
- console.log(Validator.validateColor('#abcdef')); // true
- console.log(Validator.validateColor('#ABCDE')); // false
- console.log(Validator.validateColor('abcdef')); // false
- console.log(Validator.validateColor(0)); // false
- console.log(Validator.normalizeColor('#2aF')); // #22aaff
- console.log(Validator.normalizeColor('#27aADb')); // #27aadb
- console.log(Validator.validateEasing('linear')); // true
- console.log(Validator.validateEasing('tease')); // false
- console.log(Validator.validateTextAlignment('center')); // true
- console.log(Validator.validateTextAlignment('middle')); // false
- console.log(Validator.validateFileName('foobar')); // true
- console.log(Validator.validateFileName('foobar.txt')); // true
- console.log(Validator.validateFileName('/foo/bar')); // false
- console.log(Validator.validateFileName('../foobar')); // false
- console.log(Validator.cookieName(uniqid('cookie')); // true
- console.log(Validator.cookieName(md5(uniqid('cookie'))); // true
- console.log(Validator.cookieName('cookie' . '_' . uniqid)); // true
- console.log(Validator.cookieName('cookie' . '=' . uniqid)); // false
- console.log(Validator.modelName(uniqid('VideoModel')); // true
- console.log(Validator.modelName(uniqid('_DataModel')); // false
- </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
Comments