Validating An HTML Hex Color (not regex... i dont think)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Validating An HTML Hex Color (not regex... i dont think)

Post by s.dot »

I have a text input field that a user may enter an HTML color in. Like #FF0000, #0000FF, et cetera. How can I validate that this is a good HTML color and not just a six/7 word like 'yousuck'?

I've thought about it, and the websafe colors only use CC FF 00 33 66 99. So I could build an array of 216 colors based on that and check against it.

But I also want them to be able to put in other colors, like #9E8F3D for example. Then I remembered that all colors have an RGB value like 255,0,255 but I don't know how to convert and build an array or something to check against.

I've looked at the functions bin2hex hex2dec or the likes (cant remember exact names) but can't tell which one I need to use, or how to build something to check against.

psuedo code:

Code: Select all

if(isValidColor($_POST['color'])){
    $color = $_POST['color'];
} else {
    $color = '#000000';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

you can try this, $color is a string and it has always 7 charecters in it.using substr() you can get each charecter individually. here the first charecter is always a '#' and the next 6 are between 1 to 9 or A to F. is that what u wanted?
use array() function to create a array of elements 1 to 9 and A to F.
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Post by asgerhallas »

Why not regex?

Code: Select all

if (preg_match("/^\#[0-9A-Fa-f]{6}$/", $color)) 
{
      // the color is valid
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dechex() and hexdec() would the be ones to use here, as far as what scottayy is talking about, but I don't see a not to use regex for simple checking.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

php manual on hexdec wrote:hexdec() will ignore any non-hexadecimal characters it encounters.
as long as that behaviour is acceptable use hexdec
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

ole wrote:
php manual on hexdec wrote:hexdec() will ignore any non-hexadecimal characters it encounters.
as long as that behaviour is acceptable use hexdec
no wait you i'll have to split the string into 2 character chunks before hexdecing it. chunk_split
Post Reply