Saturday, June 4, 2011

how to validate hexadecimal color code in php

When taking Hex color code as an input from user it is necessary to validate it. We can validate using preg_match function in php which returns 1 if matches found else 0 using following code.
if(preg_match('/^#[a-f0-9]{6}$/i', $hex_color))
{
//color code is valid
....
}
else
{
//Invalid code
....
}

No comments:

Post a Comment