Jump to content

[SOLVED] Validate if Hex


monkeytooth

Recommended Posts

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

 

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 :)

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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. :P But I know in certain instances they can be shortened.

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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;
} 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.