SC: verifying user input to be a number - HOW?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

SC: verifying user input to be a number - HOW?

Post by autocart »

Hi all,
How would one go about verifying user input - function: input(topic, [notes], [default], [style=s|m|w], [cancel], [width=600], [height=400]) - to see whether the return value, which obiously is a sting, represents an integer number and has no other chars in it? It seems that XY-scripting does not offer a simple method to do that, or did I overlook something? Thx in advance.
Regards, Stephan

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: SC: verifying user input to be a number - HOW?

Post by bdeshi »

Code: Select all

 set $num;
 while ($num UnLikeI "[0-9]") || (strlen($num) != 1){
  $num = input("Enter a single numerical character","Or face the horror or the Ever-looping Inputbox!!",,s,"CancelIsNotAnEscape");
 }
With the first second condition of while you can even enforce a particular number of numbers.

EDITed out an embarrassing mistake in the snippet. :oops:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

Re: SC: verifying user input to be a number - HOW?

Post by autocart »

Thank you, Sammay,
I am glad that I overlooked something, namely the like / unlike operator.

autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

Re: SC: verifying user input to be a number - HOW?

Post by autocart »

However, it only works with a fix number of chars.
It is not flexible to work with "2 or "22" or "222" or "2222" or ....

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: verifying user input to be a number - HOW?

Post by highend »

Code: Select all

  $input = input("Enter a numerical character");
  $onlyNumber = regexmatches($input, "^\d+$");
  if ($onlyNumber) {
      text "yeah!";
  }
Even if you add a space to the input it won't trigger anymore...
One of my scripts helped you out? Please donate via Paypal

autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

Re: SC: verifying user input to be a number - HOW?

Post by autocart »

highend wrote:

Code: Select all

  $input = input("Enter a numerical character");
  $onlyNumber = regexmatches($input, "^\d+$");
  if ($onlyNumber) {
      text "yeah!";
  }
Perfect, thx, highend

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: SC: verifying user input to be a number - HOW?

Post by binocular222 »

easyway:

Code: Select all

	$input = input("Enter a numerical character");
  if($input == 0  OR  $input * 1 != 0) {echo Number}; Else {echo Text}
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

Re: SC: verifying user input to be a number - HOW?

Post by autocart »

binocular222 wrote:easyway:

Code: Select all

	$input = input("Enter a numerical character");
  if($input == 0  OR  $input * 1 != 0) {echo Number}; Else {echo Text}
yep, seems to work also. thx, bino
actually I did think of that in a similar way at first, but discarded the idea w/o trying because in comparisons (like 1 < "a", which results into 1 respectively "true") the string is (obviously) "turned into" a number.
Plus the help file says:
Strings are converted to numbers as possible (just like in comparisons).
$a=""; $b="2"; echo $a + $b; // = 2
$a="1a"; $b="2b"; echo $a + $b; // = 3
So I wrongly concluded that it would be the same with multiplication.

BTW, as I just found out (by trying it out): The help file is wrong in this point. The result in this example is not 3 but 0.

EDIT:
actually the help file says about comparisions:
Strings and Numbers

If you compare two numerical strings, they are compared as integers:

msg ("2" < "10"); // -> true! (both parts are numeric)

msg ("2" < "10a"); // -> false! (only one parts numeric)

msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
Which is weird, since 1 < "1a" results into 1 resp. "true". But 2 < "1a" results into 0 resp. "false". WEIRD!!!!!!

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC: verifying user input to be a number - HOW?

Post by PeterH »

autocart wrote:actually the help file says about comparisions:
Strings and Numbers

If you compare two numerical strings, they are compared as integers:

msg ("2" < "10"); // -> true! (both parts are numeric)

msg ("2" < "10a"); // -> false! (only one parts numeric)

msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
Which is weird, since 1 < "1a" results into 1 resp. "true". But 2 < "1a" results into 0 resp. "false". WEIRD!!!!!!
I think that's OK: it's a string compare, that is character for character, left to right, up to the first difference.
so 1... is always < than 2..., no matter what follows 1st character.
2 is > 10a because 2 > 1

So all correct?

autocart
Posts: 1386
Joined: 26 Sep 2013 15:22

Re: SC: verifying user input to be a number - HOW?

Post by autocart »

i somehow makes sense what you say, Peter. Thx.
Then, in a comparison of "real" number against non-numeric string, is the "real" number converted into a string? Obiously yes, or am I wrong?

I dunno why this is so confusing to me these days. Sorry to all, for cluttering the forum.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: SC: verifying user input to be a number - HOW?

Post by PeterH »

autocart wrote:Then, in a comparison of "real" number against non-numeric string, is the "real" number converted into a string? Obiously yes, or am I wrong?
I'd expect just the opposite: generally all variables are strings, and handled like that.
For a compare both are tested: do they just represent numbers? If yes for both they are converted and compared numerically. Else string-compare is done.

(I hope it's just as I say... :roll: )

Post Reply