Batch check for valid HTML color #*-asterisk

August 29, 2010 at 08:47:00
Specs: Windows 7, 6.0 GB RAM
I have a windows batch program that asks for a valid HTML color in HEX-notation. (ex. #000000)

Here's the issue: I need the batch program to check that the user has put the '#' in the front of the number. The entered value also must be 7 characters long (#+6 numbers). I tried using a '*' (asterisk), but it didn't for out.

I have now:
if /I "%color%" EQU "#*" goto:color_set

This does not work. It only finishes if the user enters '#*'. This is not what I want. Any help is appreciated

See More: Batch check for valid HTML color #*-asterisk

Report •

August 29, 2010 at 08:50:52
Try this:

IF "%color:~0,1%"=="#" GOTO color_set

Report •

August 29, 2010 at 23:13:49
download GNU grep for windows ,

C:\test>echo     #000000 |grep -P "^[ \t]*#.{7}"

C:\test>echo %errorlevel%

I have assumed that if there are also blanks in front of the "#" character, it is also valid. For that, the batch code Matt supplied will fail.

GNU win32 packages | Gawk

Report •

August 30, 2010 at 08:29:06
GhostDog, I want to do this strictly with BATCH. I cannot use external files for this. I'm sure that there is a way to do it in batch, I just haven't gotten it yet. Matt123, this did not quite work. If I enter "#000000" the script does not recognize it. Also, I don't mind if the script passes "# ". I don't need my script to be "full proof" against all idiots.

Thankyou for the help thus far.

Report •

Related Solutions

August 30, 2010 at 16:55:08
What exactly isn't working?

The code I provided will check to see if the first character of the entry is "#" and if it is it will "GOTO color_set".

Post a little bit more of your code, as it might be somewhere else that it craps out.

But ghostdog is right, his is more stable.

Can I put a question here?

Report •

August 31, 2010 at 10:44:36
Wow... I messed up! "color_set" didn't exist! I checked and it works perfectly. Many, Many thanks to Matt123. Also ghostdog. Thankyou all for helping me. I am very sorry for messing this up!

Report •

August 31, 2010 at 11:05:06
One more thing...

Using this

Is there any way to check if the last 6 characters are numbers? I already have something that checks the length if it. I just need to make sure numbers are entered.

Report •

August 31, 2010 at 15:35:06
Well editing computer hope user sidewinders post and incorporating my original post:

IF "%color:~0,1%"=="#" (
	ECHO %color:~1%|FINDSTR /r "[^0-9]" > nul
	IF "%ErrorLevel%"=="1" GOTO color_set
GOTO Start

Can I put a question here?

Report •

August 31, 2010 at 18:10:48

>> ECHO %color:~1%|FINDSTR /r "[^0-9]"

that will check for anything not numbers, but does not actually check for number of digits. This is more stringent test

C:\test>echo 12356| FINDSTR /r "^[0-9][0-9][0-9][0-9][0-9]$"

C:\test>echo 123456| FINDSTR /r "^[0-9][0-9][0-9][0-9][0-9]$"

GNU win32 packages | Gawk

Report •

August 31, 2010 at 18:58:40
Ahh, my bad. I never tested it. This will should work though! Thanks ghostdog!

IF "%color:~0,1%"=="#" (
	ECHO %color:~1%|FINDSTR /r ".*^[a-zA-Z].*" >Nul
	IF "%ErrorLevel%"=="0" GOTO color_set

Can I put a question here?

Report •

August 31, 2010 at 19:56:47
that should be
ECHO %color:~1%|FINDSTR /r ".*[^a-zA-Z].*" 

right ?

and it will pass these situations
1) if the spaces after and before
2) if number of digits are longer than 6.
3) if there are special characters

even though it should not.

C:\test>ECHO 123456 |FINDSTR /r ".*[^a-zA-Z].*"

C:\test>ECHO      123456 |FINDSTR /r ".*[^a-zA-Z].*"

C:\test>ECHO 12345677899|FINDSTR /r ".*[^a-zA-Z].*"

C:\test>ECHO 12345-|FINDSTR /r ".*[^a-zA-Z].*"

GNU win32 packages | Gawk

Report •

August 31, 2010 at 20:07:07
Yes, you are right. He had stated that he had already figures out a way to check if the entry was 6 digits.

Also, yeah, the escape character was in the wrong spot.


SMTS, use ghostdog's code a few posts above, it does it all.


2-in-a-row! D= guess it's bed time for me.

Can I put a question here?

Report •

August 31, 2010 at 20:38:19
Edit: Made the script slightly better...

I know I'm barging in here, but Isn't this hex? So are a-f valid numbers?

Here is something I would consider to be "idiot proof", although a little bit longer:

Tested only on xp

@echo off

set color=
set /p color=Give me a html color code(#nnnnnn)

if not defined color goto input
if "%color:"=%"=="" goto input

echo "%color:"=%"|findstr /i /r /x /c:"\"#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]\"" > nul

if errorlevel 1 goto input

set color=%color:"=%

echo the selected color is: %color%

It's case insensitive but that can easily be changed. Also
leading trailing spaces are not accepted...

Report •

August 31, 2010 at 20:59:13
yes, you are correct. It should be hex.

Anyway, i tested your code. Doesn't seem to work on #999999 for example (if ever there is such a code :) )

GNU win32 packages | Gawk

Report •

August 31, 2010 at 21:11:16

Yeah a missed that, then saw a better way to validate anyway and edited my post.

FWIW all that was wrong is that I missed "789" from my delims(now not even part of the script)....

Report •

September 1, 2010 at 11:55:18
Thank you guys so much for all the help! It is much appreciated.

Report •

Ask Question