Skip to content

Instantly share code, notes, and snippets.

@tylerpaige
Created April 5, 2018 16:23
Show Gist options
  • Save tylerpaige/5780581be7d1d919797c87e92d1a7d1c to your computer and use it in GitHub Desktop.
Save tylerpaige/5780581be7d1d919797c87e92d1a7d1c to your computer and use it in GitHub Desktop.
Valid RGBA checker
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Valid RGBA checker">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
'use strict';
function isValidRGBA(input) {
var pattern = new RegExp(/rgba\(((\d{1,3},\s*){3})(0?(\.\d+)?|(1(\.0+)?))\)/g);
var matches = pattern.exec(input);
if (matches && matches.length >= 3) {
var rgbCheck = matches[1].split(/,\s*/).slice(0, 3).every(function (v) {
return v <= 255;
});
console.log(input + ': ' + rgbCheck);
return rgbCheck;
} else {
console.log(input + ': ' + false);
return false;
}
}
/* Expected TRUEs*/
console.log('Expect TRUEs');
isValidRGBA('rgba(123, 123, 123, 1)');
isValidRGBA('rgba(123, 123, 123, 0)');
isValidRGBA('rgba(123, 123, 123, 0.5)');
isValidRGBA('rgba(123, 123, 123, 0.9999999)');
isValidRGBA('rgba(123, 123, 123, .9999999)');
isValidRGBA('rgba(1, 12, 123, 0.5)');
isValidRGBA('rgba(0, 0, 0, 0.0)');
isValidRGBA('rgba(1, 1, 1, 1)');
isValidRGBA('rgba(1, 1, 1, 1.0)');
isValidRGBA('rgba(1, 1, 1, 1.00000)');
isValidRGBA('rgba(123,123,123,1)');
isValidRGBA('rgba(123,123, 123,1)');
console.log('\nExpect FALSEs');
/* Expected FALSEs */
isValidRGBA('rgba(1, 1, 1, 1.)');
isValidRGBA('rgba(0, 255, 511, 1)');
isValidRGBA('rgba(1, 1, 1, 2)');
isValidRGBA('rgba(1, 1, 1, 1');
isValidRGBA('rgba(1, -1, 1, 1)');
</script>
<script id="jsbin-source-javascript" type="text/javascript">function isValidRGBA(input) {
const pattern = new RegExp(/rgba\(((\d{1,3},\s*){3})(0?(\.\d+)?|(1(\.0+)?))\)/g);
const matches = pattern.exec(input);
if (matches && matches.length >= 3) {
const rgbCheck = matches[1].split(/,\s*/).slice(0, 3).every(v => v <= 255);
console.log(`${input}: ${rgbCheck}`);
return rgbCheck;
} else {
console.log(`${input}: ${false}`);
return false;
}
}
/* Expected TRUEs*/
console.log('Expect TRUEs');
isValidRGBA('rgba(123, 123, 123, 1)');
isValidRGBA('rgba(123, 123, 123, 0)');
isValidRGBA('rgba(123, 123, 123, 0.5)');
isValidRGBA('rgba(123, 123, 123, 0.9999999)');
isValidRGBA('rgba(123, 123, 123, .9999999)');
isValidRGBA('rgba(1, 12, 123, 0.5)');
isValidRGBA('rgba(0, 0, 0, 0.0)');
isValidRGBA('rgba(1, 1, 1, 1)');
isValidRGBA('rgba(1, 1, 1, 1.0)');
isValidRGBA('rgba(1, 1, 1, 1.00000)');
isValidRGBA('rgba(123,123,123,1)');
isValidRGBA('rgba(123,123, 123,1)');
console.log('\nExpect FALSEs');
/* Expected FALSEs */
isValidRGBA('rgba(1, 1, 1, 1.)');
isValidRGBA('rgba(0, 255, 511, 1)');
isValidRGBA('rgba(1, 1, 1, 2)');
isValidRGBA('rgba(1, 1, 1, 1');
isValidRGBA('rgba(1, -1, 1, 1)');</script></body>
</html>
'use strict';
function isValidRGBA(input) {
var pattern = new RegExp(/rgba\(((\d{1,3},\s*){3})(0?(\.\d+)?|(1(\.0+)?))\)/g);
var matches = pattern.exec(input);
if (matches && matches.length >= 3) {
var rgbCheck = matches[1].split(/,\s*/).slice(0, 3).every(function (v) {
return v <= 255;
});
console.log(input + ': ' + rgbCheck);
return rgbCheck;
} else {
console.log(input + ': ' + false);
return false;
}
}
/* Expected TRUEs*/
console.log('Expect TRUEs');
isValidRGBA('rgba(123, 123, 123, 1)');
isValidRGBA('rgba(123, 123, 123, 0)');
isValidRGBA('rgba(123, 123, 123, 0.5)');
isValidRGBA('rgba(123, 123, 123, 0.9999999)');
isValidRGBA('rgba(123, 123, 123, .9999999)');
isValidRGBA('rgba(1, 12, 123, 0.5)');
isValidRGBA('rgba(0, 0, 0, 0.0)');
isValidRGBA('rgba(1, 1, 1, 1)');
isValidRGBA('rgba(1, 1, 1, 1.0)');
isValidRGBA('rgba(1, 1, 1, 1.00000)');
isValidRGBA('rgba(123,123,123,1)');
isValidRGBA('rgba(123,123, 123,1)');
console.log('\nExpect FALSEs');
/* Expected FALSEs */
isValidRGBA('rgba(1, 1, 1, 1.)');
isValidRGBA('rgba(0, 255, 511, 1)');
isValidRGBA('rgba(1, 1, 1, 2)');
isValidRGBA('rgba(1, 1, 1, 1');
isValidRGBA('rgba(1, -1, 1, 1)');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment