[thelist] hex colour check (javascript)
Liorean
Liorean at user.bip.net
Thu Sep 26 08:04:01 CDT 2002
At 08:48 2002-09-24 +0100, Simon Hibbard wrote:
>Hi all,
>Currently have a form allowing users to enter a hex colour value which is
>used to update
>various styles. What I'm after is a way to validate that what they enter,
>i check the length
>and the fact that it starts with '#', but any ideas on how to check the
>hex is valid i.e.
>#000000 to #FFFFFF.
>I tried using 'parseInt(val,16)' and to check in 'isNan' but such strings
>as FFFFFS still do
>not return 'NaN'.
>I suppose i could check each letter is one of A,B,C,D,E,F - surely there
>is a better way?
>Ideas?
Well, why not use regex for this?
var re=/^#([0-9a-f]{1,2}){3}$/i;
var t=re.exec(colour);
alert('Valid colour: '+re.test(colour)+'\nRed: '+t[1]+'
('+parseInt(t[1],16)+')\nGreen: '+t[2]+' ('+parseInt(t[2],16)+')\nBlue:
'+t[3]+' ('+parseInt(t[3],16)+')');
// Liorean
More information about the thelist
mailing list