Created
April 5, 2018 16:23
-
-
Save tylerpaige/5780581be7d1d919797c87e92d1a7d1c to your computer and use it in GitHub Desktop.
Valid RGBA checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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