Skip to content

Commit 2d828de

Browse files
committed
add validator for colors - HSLA
1 parent 4def6f3 commit 2d828de

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/modules/color.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,65 @@
186186
errorMessageKey: 'badHsl'
187187
});
188188

189+
/**
190+
* Check HSLA format
191+
*/
192+
$.formUtils.addValidator({
193+
name: 'hsla',
194+
validatorFunction: function(val, $el) {
195+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
196+
return true;
197+
}
198+
199+
var isInRange;
200+
var removedSpace = val.replace(/ /g, '');
201+
var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]{1}.?[0-9]*\)/i;
202+
203+
if (removedSpace.match(regex)) {
204+
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
205+
var valueSliced = removeBrackets.split(',');
206+
var isValid = true;
207+
208+
valueSliced.forEach(function(i, index) {
209+
var value = filterFloat(i);
210+
211+
if (Number.isInteger(value)) {
212+
213+
if (index === 0) {
214+
isInRange = 0 <= value && value <= 360;
215+
if (!isInRange) {
216+
isValid = false;
217+
}
218+
} else {
219+
220+
isInRange = 0 <= value && value <= 100;
221+
if (!isInRange) {
222+
isValid = false;
223+
}
224+
}
225+
} else {
226+
if (isNaN(value)) {
227+
// percent value
228+
value = parseInt(i);
229+
isInRange = 0 <= value && value <= 100;
230+
if (!isInRange) {
231+
isValid = false;
232+
}
233+
} else {
234+
isInRange = 0 <= value && value <= 1;
235+
if (!isInRange) {
236+
isValid = false;
237+
}
238+
}
239+
}
240+
});
241+
return isValid;
242+
}
243+
244+
return false;
245+
},
246+
errorMessage: '',
247+
errorMessageKey: 'badHsla'
248+
});
249+
189250
})(jQuery);

0 commit comments

Comments
 (0)