Skip to content

Commit 2f34193

Browse files
committed
improvement color validation
1 parent 6bc133b commit 2f34193

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

form-validator/color.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/color.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@
7777
}
7878

7979
var removedSpace = val.replace(/ /g, '');
80-
var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i;
80+
var regex = /rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i;
8181

8282
if (removedSpace.match(regex)) {
83-
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
83+
var removeRgbCall = removedSpace.replace(/rgb/g, '');
84+
var removeBrackets = removeRgbCall.replace(/\(/g, '').replace(/\)/g, '');
8485
var valueSliced = removeBrackets.split(',');
8586
var isValid = true;
8687

8788
valueSliced.forEach(function(i) {
88-
var parsedInt = parseInt(i);
89+
var parsedInt = parseInt(i, 10);
8990
if ((Number.isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
9091
isValid = false;
9192
}
@@ -110,24 +111,27 @@
110111
}
111112

112113
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+
var regex = /rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]?.?[0-9]*\)/i;
114115

115116
if (removedSpace.match(regex)) {
116-
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
117+
var removeRgbaCall = removedSpace.replace(/rgba/g, '');
118+
var removeBrackets = removeRgbaCall.replace(/\(/g, '').replace(/\)/g, '');
117119
var valueSliced = removeBrackets.split(',');
118120
var isValid = true;
119121

120-
valueSliced.forEach(function(i) {
122+
valueSliced.forEach(function(i, index) {
121123
var value = filterFloat(i);
122124
if (Number.isInteger(value)) {
123125
var isInRange = value >= 0 && value <= 255;
124126
if (!isInRange) {
125127
isValid = false;
126128
}
127-
} else {
128-
if (!isBetween0and1(value)) {
129-
isValid = false;
129+
130+
if (isValid && index === 3) {
131+
isValid = value >= 0 && value < 2;
130132
}
133+
} else if (!isBetween0and1(i)) {
134+
isValid = false;
131135
}
132136
});
133137
return isValid;
@@ -149,26 +153,21 @@
149153
return true;
150154
}
151155

152-
var isInRange;
153156
var removedSpace = val.replace(/ /g, '');
154-
var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i;
157+
var regex = /hsl\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i;
155158

156159
if (removedSpace.match(regex)) {
157-
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
160+
var removeHslCall = removedSpace.replace(/hsl/g, '');
161+
var removeBrackets = removeHslCall.replace(/\(/g, '').replace(/\)/g, '');
158162
var valueSliced = removeBrackets.split(',');
159163
var isValid = true;
160164

161165
valueSliced.forEach(function(i, index) {
162-
var parsedInt = parseInt(i);
166+
var parsedInt = parseInt(i, 10);
163167

164168
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;
169+
if (index !== 0) {
170+
var isInRange = parsedInt >= 0 && parsedInt <= 100;
172171
if (!isInRange) {
173172
isValid = false;
174173
}
@@ -198,46 +197,44 @@
198197

199198
var isInRange;
200199
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;
200+
var regex = /hsla\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]?.?[0-9]*\)/i;
202201

203202
if (removedSpace.match(regex)) {
204-
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
203+
var removeHslaCall = removedSpace.replace(/hsla/g, '');
204+
var removeBrackets = removeHslaCall.replace(/\(/g, '').replace(/\)/g, '');
205205
var valueSliced = removeBrackets.split(',');
206206
var isValid = true;
207207

208208
valueSliced.forEach(function(i, index) {
209209
var value = filterFloat(i);
210+
var parsedInt = parseInt(i, 10);
210211

211212
if (Number.isInteger(value)) {
212-
213-
if (index === 0) {
214-
isInRange = 0 <= value && value <= 360;
213+
if (index !== 0 && index !== 3) {
214+
isInRange = value >= 0 && value <= 100;
215215
if (!isInRange) {
216216
isValid = false;
217217
}
218-
} else {
218+
}
219219

220-
isInRange = 0 <= value && value <= 100;
221-
if (!isInRange) {
222-
isValid = false;
223-
}
220+
if (isValid && index === 3) {
221+
isValid = value >= 0 && value < 2;
222+
}
223+
} else if (isNaN(value) && Number.isInteger(parsedInt)) {
224+
isInRange = parsedInt >= 0 && parsedInt <= 100;
225+
if (!isInRange) {
226+
isValid = false;
224227
}
225228
} 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-
}
229+
value = filterFloat(Number(i).toFixed(20));
230+
231+
isInRange = value >= 0 && value <= 1;
232+
if (!isInRange) {
233+
isValid = false;
238234
}
239235
}
240236
});
237+
241238
return isValid;
242239
}
243240

0 commit comments

Comments
 (0)