Only accept a numeric number.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
PAB
Posts: 118
Joined: 12 Aug 2019 13:57

Re: Only accept a numeric number.

#16 Post by PAB » 07 Aug 2020 13:58

Hello Compo,
Compo wrote:
06 Aug 2020 12:34

Why didn't you try my single line example, (which was split over multiple lines for ease of reading), instead of using those four lines?

Code: Select all

If Not Defined OS_Index (GoTo Menu)Else Set OS_Index|"%__AppDir__%findstr.exe" "^OS_Index=[123456789]$ ^OS_Index=1[012]$">NUL||GoTo Menu

Code: Select all

:Menu

Set "OS_Index="
Set /P "OS_Index=Input Index Number> "

If Not Defined OS_Index (
  GoTo Menu
) Else (
  Set OS_Index | "%__AppDir__%findstr.exe" ^
  "^OS_Index=[123456789]$ ^OS_Index=[1][012]$" 1> NUL ^
 || GoTo Menu
)

Echo OS Index number is "OS_Index" & Pause
So would the code above work and [a] ignore just pressing <Enter>, [b] ignore inputting a zero, [c] ignore a number greater than 12, [d] ignore ANY letter?

Would I use the number held in OS_Index by using "OS_Index" or something else please?

I noticed that with your code you don't use any " or !.

I haven't got access to my computer until Monday but I will then be able to test out any new code or recommendations.

Thanks in advance.

penpen
Expert
Posts: 1872
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Only accept a numeric number.

#17 Post by penpen » 08 Aug 2020 03:16

PAB wrote:
07 Aug 2020 13:31
So let me see if this is correct . . .
That source is correct.

PAB wrote:
07 Aug 2020 13:31
Then to use the number that is now stored in %OS_Index% I would just use %OS_Index% in a cmd further down the code, or would I need to use "%OS_Index%"
Is that correct please?
I just thought that once you started using !, you should carry on using them in a For loop and I didn't realise that they can both be used together, or am I wrong?
Outside of compound_statements ('(' statement_seqence ')' or statement_seqence '&' statement or loop bodies of for loops ()everything after the "do")) you can use both, normal (%) and delayed (!) expansion.
Within compound_statements it depends on what you want the environment varuables to do.
Normal expansion is applied when the entire code block is read, while the delayed expansion is performed, when a single command is executed.
Both behaviours can be usefull.

For example you could use a counter variable within a for loop and reset its original value at the end:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "i=13"
set "elements=a b c d"

(
	set "i=0"
	for %%e in (%elements%) do (
		for %%i in ("!i!") do set "elements=!elements:%%~e=%%~e[%%~i]!"
		set /a "i+=1"
	)
	
	set "i=%i%"
)

echo(i=%i%
echo(elements=%elements%
echo(i=!i!
echo(elements=!elements!

goto :eof
I hope that example clarifies the different usages (and also how you would use a changing variable within delayed expansion (done in inner for-loop).


penpen

PAB
Posts: 118
Joined: 12 Aug 2019 13:57

Re: Only accept a numeric number.

#18 Post by PAB » 08 Aug 2020 11:17

WOW!, thank you penpen for taking the time to produce the detailed reply, it is very much appreciated. It is starting to finally make some sort of sense now thanks to you and others who take the time to respond to questions, not only for me, but for everyone.

Thanks again.

Compo
Posts: 507
Joined: 21 Mar 2014 08:50

Re: Only accept a numeric number.

#19 Post by Compo » 10 Aug 2020 08:16

PAB wrote:
07 Aug 2020 13:58

Code: Select all

<snip>
Echo OS Index number is "OS_Index" & Pause
So would the code above work and [a] ignore just pressing <Enter>, [b] ignore inputting a zero, [c] ignore a number greater than 12, [d] ignore ANY letter?

Would I use the number held in OS_Index by using "OS_Index" or something else please?

I noticed that with your code you don't use any " or !.

I haven't got access to my computer until Monday but I will then be able to test out any new code or recommendations.

Thanks in advance.
The problem with using Set /P is that your end user can enter nothing at all or anything they like.

In my implementation, it doesn't matter what they enter, the code will continue asking until they type an integer value of at least 1 and not more than 12, before pressing their [ENTER] key.

There is no need to delay expansion, and the end user can, if they wish, enter ^ & > < % ! " | \ ? / etc, without it exiting/breaking the script.

You do not modify any of the code, only the last line which will be your own code, everything else, (especially the input line which specifies the range of entries allowed), remains exactly as it is.

To use my code this is what I'd expect, (long line version):

Code: Select all

:Menu
Set "OS_Index="
Set /P "OS_Index=Input Index Number [1..12]>"
If Not Defined OS_Index (GoTo Menu)Else Set OS_Index|"%__AppDir__%findstr.exe" "^OS_Index=[123456789]$ ^OS_Index=1[012]$">NUL||GoTo Menu

Echo You entered %OS_Index% as your OS Index number. & Pause
and, (split line version):

Code: Select all

:Menu
Set "OS_Index="
Set /P "OS_Index=Input Index Number [1..12]>"

If Not Defined OS_Index (GoTo Menu)Else (
	Set OS_Index | "%__AppDir__%findstr.exe" ^
	"^OS_Index=[123456789]$ ^OS_Index=[1][012]$" 1> NUL ^
	|| GoTo Menu)

Echo You entered %OS_Index% as your OS Index number. & Pause
The Index number entered by the end user will be held in the variable named OS_Index, the value of which will be %OS_Index%, (as displayed on the example last line).

Aacini
Expert
Posts: 1676
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Only accept a numeric number.

#20 Post by Aacini » 10 Aug 2020 09:21

The problem with using Set /P is that your end user can enter nothing at all or anything they like. For this reason you should not use Set /P at all...

In my implementation, the user can enter a correct answer ONLY. If press ENTER at beginning, it is ignored. If press any key other than a digit between 1 and 9 at the first position, is is ignored. In the second position it is only allowed a digit 0, 1 or 2, but ONLY if the first digit was 1. After two digits, only the ENTER key is allowed. In other words: my solution only allows to ENTER a number between 1 and 12. This is what I think that "Only accept a numeric number" means...

It seems that my solution is exactly what you are looking for, so I don't understand why you didn't used it.

Antonio

PAB
Posts: 118
Joined: 12 Aug 2019 13:57

Re: Only accept a numeric number.

#21 Post by PAB » 11 Aug 2020 14:42

Thank you Compo and Aacini for the replies.
I will look at them in more detail in the week and reply.
Sorry I haven't replied before but I have had a couple of intensive days at the hospital having tests etc.

Post Reply