validate-css-value.js (742B)
1 function kirkiValidateCSSValue( value ) { 2 3 var validUnits = ['rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax'], 4 numericValue, 5 unit; 6 7 // 0 is always a valid value 8 if ( '0' === value ) { 9 return true; 10 } 11 12 // If we're using calc() just return true. 13 if ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) { 14 return true; 15 } 16 17 // Get the numeric value. 18 numericValue = parseFloat( value ); 19 20 // Get the unit 21 unit = value.replace( numericValue, '' ); 22 23 // Check the validity of the numeric value. 24 if ( isNaN( numericValue ) ) { 25 return false; 26 } 27 28 // Check the validity of the units. 29 if ( -1 === jQuery.inArray( unit, validUnits ) ) { 30 return false; 31 } 32 33 return true; 34 35 }