monkeytooth Posted February 3, 2008 Share Posted February 3, 2008 ok, this may be a stupid question.. But lets say I wanted a function similar to an email validation function.. in retrospect to the concept of figuring out if it is valid format. How would I do that with a color hex, including the # (or not if need be), ive done it with email, but thats a fairly easy concept.. If someone types in #00ff00 i need to determine a) if the string is long enough, b) if its a valid combination.. and if not return false.. Quote Link to comment Share on other sites More sharing options...
Stooney Posted February 3, 2008 Share Posted February 3, 2008 Well as far as I know (and I could very well be wrong), any 6 character combination is a 'valid hex value'. As far as testing it's length, you could use strlen(). if(strlen($hex)==7){ //7 is the '#' + the 6 characters //correct } Quote Link to comment Share on other sites More sharing options...
laffin Posted February 3, 2008 Share Posted February 3, 2008 preg_match('/#?[0-9a-f]{6}/',$color); shud do it... Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 3, 2008 Share Posted February 3, 2008 Hexadecimal colour codes can be 3 or 6 digits long, wouldn't the code you provided only work if it was 6. i.e. if you have F0F0F0 (Red), because the first 2, the second 2 and the third 2 are identical, you can (and should) shorten it to F0F. I believe... preg_match('/#?[0-9a-f]{3|6}/',$color); .. will be better. Quote Link to comment Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 I really think the simpler method is to provide only acceptable choices to the user. Most 'users' haven't got a clue about RGB triplets. If you don't want to use a javascript color picker, use a dropdown like the attached for acceptable choices. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 i.e. if you have F0F0F0 (Red), because the first 2, the second 2 and the third 2 are identical, you can (and should) shorten it to F0F. Assuming we're talking about RGB triplets, then F0F0F0 is a very pale grey. F0F is short for FF00FF - a lurid purple. Who knows how good a 'user' might be with entering hex codes Quote Link to comment Share on other sites More sharing options...
Barand Posted February 4, 2008 Share Posted February 4, 2008 a lurid purple. Magenta. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 4, 2008 Share Posted February 4, 2008 Meh, well I don't know hex codes off by heart. But I know in certain instances they can be shortened. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted February 4, 2008 Author Share Posted February 4, 2008 Well first off I want to thank everyone on the replies.. very helpful indeed.. and yes I know, I should only offer up the simple select few.. but I ultimately want to avoid having to deal with the person I'm making this app for, coming back to me and saying I really want to do this color but I can't.. (all cause he found a HEX code on the net to a pretty color).. I am however implementing a color picker.. and that works fine, its just a matter of allowing this guy to edit manually should he choose to do so.. So in letting him do so I need to implement a few things here and there to debunk any possible errors, though that in it of itself is a chore, cause we all know if they don't know they will try to do it anyway an find away to frig it all up.. I'm just trying to avoid the simpler problems lol.. don't call me unless the app completely shuts down so to speak.. otherwise its a quick fix this is how have a nice day.. Quote Link to comment Share on other sites More sharing options...
revraz Posted February 4, 2008 Share Posted February 4, 2008 It wasn't the color that was wrong (it was but doesnt matter), but the example you gave was wrong. You stated F0F0F0 could be set to F0F, when in fact they have to be in pairs to be shortened, like FF00FF is F0F. Meh, well I don't know hex codes off by heart. But I know in certain instances they can be shortened. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted February 4, 2008 Author Share Posted February 4, 2008 If i were to preg_match in an if statement, would it be safe to assume this is the right way if (preg_match('/#?[0-9a-f]{3|6}/',$_POST['T5'])) { echo "good"; } else { echo "bad" } I think I have something a bit mixed up.. Even when I flip good to bad and vise versa, i still get a bad result.. So what I'm tempting to find out is, is the preg matching a string equivalent to #000000 or is it matching to something else? if so How could I make it match to the equivalent of #000000 (# with 6 char's, not black) Quote Link to comment Share on other sites More sharing options...
revraz Posted February 4, 2008 Share Posted February 4, 2008 Why not use ctype? http://us.php.net/manual/en/function.ctype-xdigit.php and just check the length? Quote Link to comment Share on other sites More sharing options...
Stooney Posted February 4, 2008 Share Posted February 4, 2008 If you block #000000 the user can still enter #111111 which pretty much just as black. Just fyi. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted February 4, 2008 Author Share Posted February 4, 2008 If you block #000000 the user can still enter #111111 which pretty much just as black. Just fyi. Oh I know, Im not trying to find a specific color to do anything, Im trying to find if the hex string is valid web format #000000 being the example.. I need it to find if the string given contains the # and 3 or 6 (more so 6 then 3) numbers/letters after the #.. All i know is that HEX for web is.. a combination of 0-9 and a-f, 3 or 6 digits long with a # in front of it.. So I am trying to find a way, learn a way, figure out a way to make it so I can have it find out if it is or it isnt a valid string along those specifics.. and if its not then return to me as a false. Why not use ctype? Never heard that, but I will go read through that link now to see if it can help any.. thanks Quote Link to comment Share on other sites More sharing options...
Stooney Posted February 4, 2008 Share Posted February 4, 2008 It might work better to not check for the '#'. I'd say if they enter #, strip it out of the string then add it later when you need it. $color='#F0F0F0'; $color=str_replace("#", "", $color); Then validate the the hex value: $userinput='F0F0F0'; $pattern = '~^[a-f0-9]{6,6}$~i'; if(preg_match($pattern, $userinput)){ //valid } Quote Link to comment Share on other sites More sharing options...
revraz Posted February 4, 2008 Share Posted February 4, 2008 Here is an nice example from the page I referenced above function check_valid_colorhex($colorCode) { // If user accidentally passed along the # sign, strip it off $colorCode = ltrim($colorCode, '#'); if ( ctype_xdigit($colorCode) && (strlen($colorCode) == 6 || strlen($colorCode) == 3)) return true; else return false; } Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted February 4, 2008 Author Share Posted February 4, 2008 Alright I found my happy medium!! Again thanks to all for the help.. A little from every post has allowed me to construct something that works to my desired effect.. Special thanks to revraz for the link, that was extremely helpful.. as well as chrisdburns between the 2 of you your posts helped out the most, again though it was a collective of all the posts.. you guys are great.. Quote Link to comment Share on other sites More sharing options...
laffin Posted February 4, 2008 Share Posted February 4, 2008 if (preg_match('/#?[0-9a-f]{3|6}/i',$_POST['T5'])) echo 'is good'; else echo 'is bad'; remember the 'i' option for case insensitivity, other from that shud be good Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.