Skip to content

Commit 4def6f3

Browse files
committed
add validator for colors - HSL
1 parent 9a6594c commit 4def6f3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/modules/color.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,51 @@
139139
errorMessageKey: 'badRgba'
140140
});
141141

142+
/**
143+
* Check HSL format
144+
*/
145+
$.formUtils.addValidator({
146+
name: 'hsl',
147+
validatorFunction: function(val, $el) {
148+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
149+
return true;
150+
}
151+
152+
var isInRange;
153+
var removedSpace = val.replace(/ /g, '');
154+
var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i;
155+
156+
if (removedSpace.match(regex)) {
157+
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
158+
var valueSliced = removeBrackets.split(',');
159+
var isValid = true;
160+
161+
valueSliced.forEach(function(i, index) {
162+
var parsedInt = parseInt(i);
163+
164+
if (Number.isInteger(parsedInt)) {
165+
if (index === 0) {
166+
isInRange = 0 <= parsedInt && parsedInt <= 360;
167+
if (!isInRange) {
168+
isValid = false;
169+
}
170+
} else {
171+
isInRange = 0 <= parsedInt && parsedInt <= 100;
172+
if (!isInRange) {
173+
isValid = false;
174+
}
175+
}
176+
} else {
177+
isValid = false;
178+
}
179+
});
180+
return isValid;
181+
}
182+
183+
return false;
184+
},
185+
errorMessage: '',
186+
errorMessageKey: 'badHsl'
187+
});
142188

143189
})(jQuery);

0 commit comments

Comments
 (0)