Skip to content

Commit 9a6594c

Browse files
committed
add validator for colors - RGBA
1 parent ee3873a commit 9a6594c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/modules/color.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,45 @@
9999
errorMessageKey: 'badRgb'
100100
});
101101

102+
/**
103+
* Check RGBA format
104+
*/
105+
$.formUtils.addValidator({
106+
name: 'rgba',
107+
validatorFunction: function(val, $el) {
108+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
109+
return true;
110+
}
111+
112+
var removedSpace = val.replace(/ /g, '');
113+
var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]{1}.?[0-9]*\)/i;
114+
115+
if (removedSpace.match(regex)) {
116+
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
117+
var valueSliced = removeBrackets.split(',');
118+
var isValid = true;
119+
120+
valueSliced.forEach(function(i) {
121+
var value = filterFloat(i);
122+
if (Number.isInteger(value)) {
123+
var isInRange = value >= 0 && value <= 255;
124+
if (!isInRange) {
125+
isValid = false;
126+
}
127+
} else {
128+
if (!isBetween0and1(value)) {
129+
isValid = false;
130+
}
131+
}
132+
});
133+
return isValid;
134+
}
135+
136+
return false;
137+
},
138+
errorMessage: '',
139+
errorMessageKey: 'badRgba'
140+
});
141+
102142

103143
})(jQuery);

0 commit comments

Comments
 (0)