Skip to content

Commit ee3873a

Browse files
committed
add validator for colors - RGB
1 parent 01ebd5a commit ee3873a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/modules/color.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515

1616
$.formUtils.registerLoadedModule('color');
1717

18+
/*
19+
HELPER FUNCTIONS
20+
*/
21+
var filterFloat = function(value) {
22+
if (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
23+
.test(value)) {
24+
return Number(value);
25+
}
26+
27+
return NaN;
28+
};
29+
30+
var isBetween0and1 = function(value) {
31+
return value > 0 && value < 1;
32+
};
33+
1834
/**
1935
* Check HEX format
2036
*/
@@ -50,7 +66,38 @@
5066
errorMessageKey: 'badHex'
5167
});
5268

69+
/**
70+
* Check RGB format
71+
*/
72+
$.formUtils.addValidator({
73+
name: 'rgb',
74+
validatorFunction: function(val, $el) {
75+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
76+
return true;
77+
}
78+
79+
var removedSpace = val.replace(/ /g, '');
80+
var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i;
5381

82+
if (removedSpace.match(regex)) {
83+
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
84+
var valueSliced = removeBrackets.split(',');
85+
var isValid = true;
86+
87+
valueSliced.forEach(function(i) {
88+
var parsedInt = parseInt(i);
89+
if ((Number.isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
90+
isValid = false;
91+
}
92+
});
93+
return isValid;
94+
}
95+
96+
return false;
97+
},
98+
errorMessage: '',
99+
errorMessageKey: 'badRgb'
100+
});
54101

55102

56103
})(jQuery);

0 commit comments

Comments
 (0)