Skip to content

Color validator improvement #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion form-validator/color.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 50 additions & 46 deletions src/modules/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
return value > 0 && value < 1;
};

// workaround for PhantomJS
// https://github.com/ariya/phantomjs/issues/14014
// can't use Number.isInteger
var isInteger = function(value) {
return Math.floor(value) === value && $.isNumeric(value);
};

/**
* Check HEX format
*/
Expand Down Expand Up @@ -77,16 +84,17 @@
}

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

if (removedSpace.match(regex)) {
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
var removeRgbCall = removedSpace.replace(/rgb/g, '');
var removeBrackets = removeRgbCall.replace(/\(/g, '').replace(/\)/g, '');
var valueSliced = removeBrackets.split(',');
var isValid = true;

valueSliced.forEach(function(i) {
var parsedInt = parseInt(i);
if ((Number.isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
var parsedInt = parseInt(i, 10);
if ((isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
isValid = false;
}
});
Expand All @@ -110,24 +118,27 @@
}

var removedSpace = val.replace(/ /g, '');
var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]{1}.?[0-9]*\)/i;
var regex = /rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]?.?[0-9]*\)/i;

if (removedSpace.match(regex)) {
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
var removeRgbaCall = removedSpace.replace(/rgba/g, '');
var removeBrackets = removeRgbaCall.replace(/\(/g, '').replace(/\)/g, '');
var valueSliced = removeBrackets.split(',');
var isValid = true;

valueSliced.forEach(function(i) {
valueSliced.forEach(function(i, index) {
var value = filterFloat(i);
if (Number.isInteger(value)) {
if (isInteger(value)) {
var isInRange = value >= 0 && value <= 255;
if (!isInRange) {
isValid = false;
}
} else {
if (!isBetween0and1(value)) {
isValid = false;

if (isValid && index === 3) {
isValid = value >= 0 && value < 2;
}
} else if (!isBetween0and1(i)) {
isValid = false;
}
});
return isValid;
Expand All @@ -149,26 +160,21 @@
return true;
}

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

if (removedSpace.match(regex)) {
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
var removeHslCall = removedSpace.replace(/hsl/g, '');
var removeBrackets = removeHslCall.replace(/\(/g, '').replace(/\)/g, '');
var valueSliced = removeBrackets.split(',');
var isValid = true;

valueSliced.forEach(function(i, index) {
var parsedInt = parseInt(i);
var parsedInt = parseInt(i, 10);

if (Number.isInteger(parsedInt)) {
if (index === 0) {
isInRange = 0 <= parsedInt && parsedInt <= 360;
if (!isInRange) {
isValid = false;
}
} else {
isInRange = 0 <= parsedInt && parsedInt <= 100;
if (isInteger(parsedInt)) {
if (index !== 0) {
var isInRange = parsedInt >= 0 && parsedInt <= 100;
if (!isInRange) {
isValid = false;
}
Expand Down Expand Up @@ -198,46 +204,44 @@

var isInRange;
var removedSpace = val.replace(/ /g, '');
var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]{1}.?[0-9]*\)/i;
var regex = /hsla\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]?.?[0-9]*\)/i;

if (removedSpace.match(regex)) {
var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, '');
var removeHslaCall = removedSpace.replace(/hsla/g, '');
var removeBrackets = removeHslaCall.replace(/\(/g, '').replace(/\)/g, '');
var valueSliced = removeBrackets.split(',');
var isValid = true;

valueSliced.forEach(function(i, index) {
var value = filterFloat(i);
var parsedInt = parseInt(i, 10);

if (Number.isInteger(value)) {

if (index === 0) {
isInRange = 0 <= value && value <= 360;
if (isInteger(value)) {
if (index !== 0 && index !== 3) {
isInRange = value >= 0 && value <= 100;
if (!isInRange) {
isValid = false;
}
} else {
}

isInRange = 0 <= value && value <= 100;
if (!isInRange) {
isValid = false;
}
if (isValid && index === 3) {
isValid = value >= 0 && value < 2;
}
} else if (isNaN(value) && isInteger(parsedInt)) {
isInRange = parsedInt >= 0 && parsedInt <= 100;
if (!isInRange) {
isValid = false;
}
} else {
if (isNaN(value)) {
// percent value
value = parseInt(i);
isInRange = 0 <= value && value <= 100;
if (!isInRange) {
isValid = false;
}
} else {
isInRange = 0 <= value && value <= 1;
if (!isInRange) {
isValid = false;
}
value = filterFloat(Number(i).toFixed(20));

isInRange = value >= 0 && value <= 1;
if (!isInRange) {
isValid = false;
}
}
});

return isValid;
}

Expand Down
145 changes: 144 additions & 1 deletion test/qunit.html
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,154 @@
});
});

test("Color Hex validation", function() {

clearForm();

var links = [
{val: '#0000FF', isValid: true},
{val: '#00F', isValid: true},
{val: '123', isValid: false},
{val: '112233', isValid: false},
{val: '#4567', isValid: false},
{val: input('transparent', {'allow-transparent': 'true'}), isValid: true},
{val: 'transparent', isValid: false}
];

$.each(links, function(i, obj) {
runTest(obj, 'hex');
});
});

test("Color Rgb validation", function() {

clearForm();

var links = [
{val: 'rgb(255,255,255)', isValid: true},
{val: 'rgb( 255 , 255 , 255 )', isValid: true},
{val: 'rgb( 255, 255, 255 )', isValid: true},
{val: 'rgb(255,255,255)', isValid: true},


{val: 'rgb(-10,255,255)', isValid: false},
{val: '255255255', isValid: false},
{val: 'rgb(255,255,256)', isValid: false},


{val: input('transparent', {'allow-transparent': 'true'}), isValid: true},
{val: 'transparent', isValid: false}
];

$.each(links, function(i, obj) {
runTest(obj, 'rgb');
});
});

test("Color Rgba validation", function() {

clearForm();

var links = [
{val: 'rgba(255,255,255,1)', isValid: true},
{val: 'rgba( 255 , 255 , 255 , 1 )', isValid: true},
{val: 'rgba( 255 , 255 , 255 , 1 )', isValid: true},
{val: 'rgba(255,255,255,1)', isValid: true},
{val: 'rgba(255,255,255,0)', isValid: true},
{val: 'rgba(255,255,255,1)', isValid: true},
{val: 'rgba(255,255,255,0.5)', isValid: true},
{val: 'rgba(255,255,255,.5)', isValid: true},
{val: 'rgba(255,255,255,.524141)', isValid: true},
{val: 'rgba(255,255,255,2)', isValid: false},
{val: 'rgba(255,255,255,-1)', isValid: false},
{val: 'rgba(255,255,255,1.000000000001)', isValid: false},
{val: 'rgba(255,255,255,-0.5)', isValid: false},
{val: 'rgba(255,255,255,2.3)', isValid: false},
{val: 'rgba(-10,255,255,1)', isValid: false},
{val: '2552552551', isValid: false},
{val: 'rgba(255,255,256),1', isValid: false},
{val: '0000FF', isValid: false},
{val: input('transparent', {'allow-transparent': 'true'}), isValid: true},
{val: 'transparent', isValid: false}
];

$.each(links, function(i, obj) {
runTest(obj, 'rgba');
});
});

test("Color Hsl validation", function() {

clearForm();

var links = [
{val: 'hsl(120,50%,50%)', isValid: true},
{val: 'hsl( 120 , 50% , 50% )', isValid: true},
{val: 'hsl( 120, 50%, 50% )', isValid: true},
{val: 'hsl(-120,50%,50%)', isValid: true},
{val: 'hsl(480,50%,50%)', isValid: true},

{val: 'hsl(10,-50%,50%)', isValid: false},
{val: 'hsl(10,50%,-50%)', isValid: false},
{val: '120,50%,50%', isValid: false},
{val: 'hsl(120,100%,101%)', isValid: false},
{val: 'hsl(50%, 50%, 100%)', isValid: false},
{val: 'hsl(120, 50, 100%)', isValid: false},
{val: 'hsl(120, 50%, 100)', isValid: false},

{val: input('transparent', {'allow-transparent': 'true'}), isValid: true},
{val: 'transparent', isValid: false}
];

$.each(links, function(i, obj) {
runTest(obj, 'hsl');
});
});

test("Color Hsla validation", function() {

clearForm();

var links = [
{val: 'hsla(120,50%,50%,1)', isValid: true},
{val: 'hsla( 120 , 50% , 50%, 1 )', isValid: true},
{val: 'hsla( 120, 50%, 50% , 1 )', isValid: true},
{val: 'hsla(-120,50%,50%,1)', isValid: true},
{val: 'hsla(480,50%,50%,1)', isValid: true},
{val: 'hsla(120,50%,100%,0)', isValid: true},
{val: 'hsla(120,50%,100%,1)', isValid: true},
{val: 'hsla(120,50%,100%,0.5)', isValid: true},
{val: 'hsla(120,50%,100%,.5)', isValid: true},
{val: 'hsla(120,50%,100%,.524141)', isValid: true},

{val: 'hsla(120,50%,100%,50%)', isValid: false},
{val: 'hsla(120,50%,100%,2)', isValid: false},
{val: 'hsla(120,50%,100%,-1)', isValid: false},
{val: 'hsla(120,50%,100%,1.000000000001)', isValid: false},
{val: 'hsla(120,50%,100%,-0.5)', isValid: false},
{val: 'hsla(120,50%,100%,2.3)', isValid: false},
{val: 'hsla(10,-50%,50%,1)', isValid: false},
{val: 'hsla(10,50%,-50%,1)', isValid: false},
{val: '120,50%,50%,1', isValid: false},
{val: 'hsla(120,100%,101%,1)', isValid: false},
{val: 'hsla(50%, 50%, 100%,1)', isValid: false},
{val: 'hsla(120, 50, 100%,1)', isValid: false},
{val: 'hsla(120, 50%, 100,1)', isValid: false},

{val: input('transparent', {'allow-transparent': 'true'}), isValid: true},
{val: 'transparent', isValid: false}
];

$.each(links, function(i, obj) {
runTest(obj, 'hsla');
});
});

// TODO: Write more tests...
}

$.validate({
modules : 'security, location, sweden, file, date, sanitize, uk, poland',
modules : 'security, location, sweden, file, date, sanitize, uk, poland, color',
onModulesLoaded: function( $form ) {
if( window.console && window.console.log )
console.log('About to run all tests');
Expand Down