One approach that comes to my mind is to pre-populate arrays with the valid css values, and then search -
var validValues = {
'textAlign': ['left', 'right', 'center', 'justify', 'start']
};
var userSpecifiedCSSValue = documet.getElementById('foo').style.textAlign.toLower();
if (validValues.textAlign.indexOf(userSpecifiedCSSValue) > -1) {
// valid
}
Note: indexOf is not available for IE <= 8.
If you want to make it generic -
var userStyle = document.getElementById('foo').style,
userSpecifiedValue,
allowedCSSValues;
for (cssProperty in userStyle) {
if (userStyle.hasOwnProperty(cssProperty) &&
validValues.hasOwnProperty(cssProperty)) {
userSpecifiedValue = userStyle[cssProperty].toLower();
allowedCSSValues = validValues[cssProperty]
if (allowedCSSValues.indexOf(userSpecifiedValue) > -1) {
// valid
} else {
// invalid
}
}
}
If you also want to include browser-specific validation, then you can create list of valid values based on the browser type. Just create another layer in your object -
var validValues = {
'chrome': {
'textAlign': []// arrays with values
}
}
However to me this still seems like a hacky solution.
text-aligncan take onlyleft, right, center, justify, start.baris invalid fortext-alignMaincontent_etc. to your control?Maincontent_is, I googled it to no avail.