Skip to content

Commit 738a84f

Browse files
committed
update color validator to not use Number.isInteger - PhantomJS workaround
1 parent b46d170 commit 738a84f

File tree

2 files changed

+283
-12
lines changed

2 files changed

+283
-12
lines changed

form-validator/color.js

Lines changed: 271 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,273 @@
1-
/** File generated by Grunt -- do not modify
2-
* JQUERY-FORM-VALIDATOR
1+
(function (root, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
// AMD. Register as an anonymous module unless amdModuleId is set
4+
define(["jquery"], function (a0) {
5+
return (factory(a0));
6+
});
7+
} else if (typeof module === 'object' && module.exports) {
8+
// Node. Does not work with strict CommonJS, but
9+
// only CommonJS-like environments that support module.exports,
10+
// like Node.
11+
module.exports = factory(require("jquery"));
12+
} else {
13+
factory(root["jQuery"]);
14+
}
15+
}(this, function (jQuery) {
16+
17+
/**
18+
* jQuery Form Validator Module: Color
19+
* ------------------------------------------
20+
* Created by dszymczuk <https://github.com/dszymczuk>
321
*
4-
* @version 2.3.77
5-
* @website http://formvalidator.net/
6-
* @author Victor Jonsson, http://victorjonsson.se
7-
* @license MIT
22+
*
23+
* This form validation module adds validators for some color formats like: hex, rgb, rgba, hsl, hsla.
24+
* Also it allow to use transparent as color
25+
* This module adds the following validators:
26+
* - color
27+
*
28+
* @license MIT
829
*/
9-
!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b(a)}):"object"==typeof module&&module.exports?module.exports=b(require("jquery")):b(a.jQuery)}(this,function(a){!function(a){a.formUtils.registerLoadedModule("color");var b=function(a){return/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(a)?Number(a):NaN},c=function(a){return a>0&&a<1};a.formUtils.addValidator({name:"hex",validatorFunction:function(a,b){if("true"===b.valAttr("allow-transparent")&&"transparent"===a)return!0;var c="#"===a[0];if(!c)return!1;var d=4===a.length||7===a.length;if(d){var e=/[0-9a-f]/i,f=a.slice(1).split(""),g=!0;return f.forEach(function(a){null===a.match(e)&&(g=!1)}),g}return!1},errorMessage:"",errorMessageKey:"badHex"}),a.formUtils.addValidator({name:"rgb",validatorFunction:function(a,b){if("true"===b.valAttr("allow-transparent")&&"transparent"===a)return!0;var c=a.replace(/ /g,""),d=/rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i;if(c.match(d)){var e=c.replace(/rgb/g,""),f=e.replace(/\(/g,"").replace(/\)/g,""),g=f.split(","),h=!0;return g.forEach(function(a){var b=parseInt(a,10);(Number.isInteger(b)&&0<=b&&b<=255)===!1&&(h=!1)}),h}return!1},errorMessage:"",errorMessageKey:"badRgb"}),a.formUtils.addValidator({name:"rgba",validatorFunction:function(a,d){if("true"===d.valAttr("allow-transparent")&&"transparent"===a)return!0;var e=a.replace(/ /g,""),f=/rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]?.?[0-9]*\)/i;if(e.match(f)){var g=e.replace(/rgba/g,""),h=g.replace(/\(/g,"").replace(/\)/g,""),i=h.split(","),j=!0;return i.forEach(function(a,d){var e=b(a);if(Number.isInteger(e)){var f=e>=0&&e<=255;f||(j=!1),j&&3===d&&(j=e>=0&&e<2)}else c(a)||(j=!1)}),j}return!1},errorMessage:"",errorMessageKey:"badRgba"}),a.formUtils.addValidator({name:"hsl",validatorFunction:function(a,b){if("true"===b.valAttr("allow-transparent")&&"transparent"===a)return!0;var c=a.replace(/ /g,""),d=/hsl\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i;if(c.match(d)){var e=c.replace(/hsl/g,""),f=e.replace(/\(/g,"").replace(/\)/g,""),g=f.split(","),h=!0;return g.forEach(function(a,b){var c=parseInt(a,10);if(Number.isInteger(c)){if(0!==b){var d=c>=0&&c<=100;d||(h=!1)}}else h=!1}),h}return!1},errorMessage:"",errorMessageKey:"badHsl"}),a.formUtils.addValidator({name:"hsla",validatorFunction:function(a,c){if("true"===c.valAttr("allow-transparent")&&"transparent"===a)return!0;var d,e=a.replace(/ /g,""),f=/hsla\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]?.?[0-9]*\)/i;if(e.match(f)){var g=e.replace(/hsla/g,""),h=g.replace(/\(/g,"").replace(/\)/g,""),i=h.split(","),j=!0;return i.forEach(function(a,c){var e=b(a),f=parseInt(a,10);Number.isInteger(e)?(0!==c&&3!==c&&(d=e>=0&&e<=100,d||(j=!1)),j&&3===c&&(j=e>=0&&e<2)):isNaN(e)&&Number.isInteger(f)?(d=f>=0&&f<=100,d||(j=!1)):(e=b(Number(a).toFixed(20)),d=e>=0&&e<=1,d||(j=!1))}),j}return!1},errorMessage:"",errorMessageKey:"badHsla"})}(a)});
30+
(function($) {
31+
32+
$.formUtils.registerLoadedModule('color');
33+
34+
/*
35+
HELPER FUNCTIONS
36+
*/
37+
var filterFloat = function(value) {
38+
if (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
39+
.test(value)) {
40+
return Number(value);
41+
}
42+
43+
return NaN;
44+
};
45+
46+
var isBetween0and1 = function(value) {
47+
return value > 0 && value < 1;
48+
};
49+
50+
// workaround for PhantomJS
51+
// https://github.com/ariya/phantomjs/issues/14014
52+
// can't use Number.isInteger
53+
var isInteger = function(value){
54+
return Math.floor(value) === value && $.isNumeric(value);
55+
};
56+
57+
/**
58+
* Check HEX format
59+
*/
60+
$.formUtils.addValidator({
61+
name: 'hex',
62+
validatorFunction: function(val, $el) {
63+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
64+
return true;
65+
}
66+
67+
var startWithHex = val[0] === '#';
68+
if (!startWithHex) {
69+
return false;
70+
}
71+
72+
var isCorrectLength = val.length === 4 || val.length === 7;
73+
74+
if (isCorrectLength) {
75+
var regex = /[0-9a-f]/i;
76+
var valueSliced = val.slice(1).split('');
77+
var isValid = true;
78+
valueSliced.forEach(function(i) {
79+
if (i.match(regex) === null) {
80+
isValid = false;
81+
}
82+
});
83+
return isValid;
84+
}
85+
86+
return false;
87+
},
88+
errorMessage: '',
89+
errorMessageKey: 'badHex'
90+
});
91+
92+
/**
93+
* Check RGB format
94+
*/
95+
$.formUtils.addValidator({
96+
name: 'rgb',
97+
validatorFunction: function(val, $el) {
98+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
99+
return true;
100+
}
101+
102+
var removedSpace = val.replace(/ /g, '');
103+
var regex = /rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i;
104+
105+
if (removedSpace.match(regex)) {
106+
var removeRgbCall = removedSpace.replace(/rgb/g, '');
107+
var removeBrackets = removeRgbCall.replace(/\(/g, '').replace(/\)/g, '');
108+
var valueSliced = removeBrackets.split(',');
109+
var isValid = true;
110+
111+
valueSliced.forEach(function(i) {
112+
var parsedInt = parseInt(i, 10);
113+
if ((isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
114+
isValid = false;
115+
}
116+
});
117+
return isValid;
118+
}
119+
120+
return false;
121+
},
122+
errorMessage: '',
123+
errorMessageKey: 'badRgb'
124+
});
125+
126+
/**
127+
* Check RGBA format
128+
*/
129+
$.formUtils.addValidator({
130+
name: 'rgba',
131+
validatorFunction: function(val, $el) {
132+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
133+
return true;
134+
}
135+
136+
var removedSpace = val.replace(/ /g, '');
137+
var regex = /rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]?.?[0-9]*\)/i;
138+
139+
if (removedSpace.match(regex)) {
140+
var removeRgbaCall = removedSpace.replace(/rgba/g, '');
141+
var removeBrackets = removeRgbaCall.replace(/\(/g, '').replace(/\)/g, '');
142+
var valueSliced = removeBrackets.split(',');
143+
var isValid = true;
144+
145+
valueSliced.forEach(function(i, index) {
146+
var value = filterFloat(i);
147+
if (isInteger(value)) {
148+
var isInRange = value >= 0 && value <= 255;
149+
if (!isInRange) {
150+
isValid = false;
151+
}
152+
153+
if (isValid && index === 3) {
154+
isValid = value >= 0 && value < 2;
155+
}
156+
} else if (!isBetween0and1(i)) {
157+
isValid = false;
158+
}
159+
});
160+
return isValid;
161+
}
162+
163+
return false;
164+
},
165+
errorMessage: '',
166+
errorMessageKey: 'badRgba'
167+
});
168+
169+
/**
170+
* Check HSL format
171+
*/
172+
$.formUtils.addValidator({
173+
name: 'hsl',
174+
validatorFunction: function(val, $el) {
175+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
176+
return true;
177+
}
178+
179+
var removedSpace = val.replace(/ /g, '');
180+
var regex = /hsl\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i;
181+
182+
if (removedSpace.match(regex)) {
183+
var removeHslCall = removedSpace.replace(/hsl/g, '');
184+
var removeBrackets = removeHslCall.replace(/\(/g, '').replace(/\)/g, '');
185+
var valueSliced = removeBrackets.split(',');
186+
var isValid = true;
187+
188+
valueSliced.forEach(function(i, index) {
189+
var parsedInt = parseInt(i, 10);
190+
191+
if (isInteger(parsedInt)) {
192+
if (index !== 0) {
193+
var isInRange = parsedInt >= 0 && parsedInt <= 100;
194+
if (!isInRange) {
195+
isValid = false;
196+
}
197+
}
198+
} else {
199+
isValid = false;
200+
}
201+
});
202+
return isValid;
203+
}
204+
205+
return false;
206+
},
207+
errorMessage: '',
208+
errorMessageKey: 'badHsl'
209+
});
210+
211+
/**
212+
* Check HSLA format
213+
*/
214+
$.formUtils.addValidator({
215+
name: 'hsla',
216+
validatorFunction: function(val, $el) {
217+
if ($el.valAttr('allow-transparent') === 'true' && val === 'transparent') {
218+
return true;
219+
}
220+
221+
var isInRange;
222+
var removedSpace = val.replace(/ /g, '');
223+
var regex = /hsla\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]?.?[0-9]*\)/i;
224+
225+
if (removedSpace.match(regex)) {
226+
var removeHslaCall = removedSpace.replace(/hsla/g, '');
227+
var removeBrackets = removeHslaCall.replace(/\(/g, '').replace(/\)/g, '');
228+
var valueSliced = removeBrackets.split(',');
229+
var isValid = true;
230+
231+
valueSliced.forEach(function(i, index) {
232+
var value = filterFloat(i);
233+
var parsedInt = parseInt(i, 10);
234+
235+
if (isInteger(value)) {
236+
if (index !== 0 && index !== 3) {
237+
isInRange = value >= 0 && value <= 100;
238+
if (!isInRange) {
239+
isValid = false;
240+
}
241+
}
242+
243+
if (isValid && index === 3) {
244+
isValid = value >= 0 && value < 2;
245+
}
246+
} else if (isNaN(value) && isInteger(parsedInt)) {
247+
isInRange = parsedInt >= 0 && parsedInt <= 100;
248+
if (!isInRange) {
249+
isValid = false;
250+
}
251+
} else {
252+
value = filterFloat(Number(i).toFixed(20));
253+
254+
isInRange = value >= 0 && value <= 1;
255+
if (!isInRange) {
256+
isValid = false;
257+
}
258+
}
259+
});
260+
261+
return isValid;
262+
}
263+
264+
return false;
265+
},
266+
errorMessage: '',
267+
errorMessageKey: 'badHsla'
268+
});
269+
270+
})(jQuery);
271+
272+
273+
}));

src/modules/color.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
return value > 0 && value < 1;
3232
};
3333

34+
// workaround for PhantomJS
35+
// https://github.com/ariya/phantomjs/issues/14014
36+
// can't use Number.isInteger
37+
var isInteger = function(value) {
38+
return Math.floor(value) === value && $.isNumeric(value);
39+
};
40+
3441
/**
3542
* Check HEX format
3643
*/
@@ -87,7 +94,7 @@
8794

8895
valueSliced.forEach(function(i) {
8996
var parsedInt = parseInt(i, 10);
90-
if ((Number.isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
97+
if ((isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
9198
isValid = false;
9299
}
93100
});
@@ -121,7 +128,7 @@
121128

122129
valueSliced.forEach(function(i, index) {
123130
var value = filterFloat(i);
124-
if (Number.isInteger(value)) {
131+
if (isInteger(value)) {
125132
var isInRange = value >= 0 && value <= 255;
126133
if (!isInRange) {
127134
isValid = false;
@@ -165,7 +172,7 @@
165172
valueSliced.forEach(function(i, index) {
166173
var parsedInt = parseInt(i, 10);
167174

168-
if (Number.isInteger(parsedInt)) {
175+
if (isInteger(parsedInt)) {
169176
if (index !== 0) {
170177
var isInRange = parsedInt >= 0 && parsedInt <= 100;
171178
if (!isInRange) {
@@ -209,7 +216,7 @@
209216
var value = filterFloat(i);
210217
var parsedInt = parseInt(i, 10);
211218

212-
if (Number.isInteger(value)) {
219+
if (isInteger(value)) {
213220
if (index !== 0 && index !== 3) {
214221
isInRange = value >= 0 && value <= 100;
215222
if (!isInRange) {
@@ -220,7 +227,7 @@
220227
if (isValid && index === 3) {
221228
isValid = value >= 0 && value < 2;
222229
}
223-
} else if (isNaN(value) && Number.isInteger(parsedInt)) {
230+
} else if (isNaN(value) && isInteger(parsedInt)) {
224231
isInRange = parsedInt >= 0 && parsedInt <= 100;
225232
if (!isInRange) {
226233
isValid = false;

0 commit comments

Comments
 (0)