Page 1 of 1

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

Posted: 05 Aug 2014 09:14
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

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

Posted: 05 Aug 2014 09:24
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:

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

Posted: 05 Aug 2014 09:43
by autocart
Thank you, Sammay,
I am glad that I overlooked something, namely the like / unlike operator.

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

Posted: 05 Aug 2014 10:18
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 ....

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

Posted: 05 Aug 2014 10:33
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...

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

Posted: 05 Aug 2014 10:55
by autocart
highend wrote:

Code: Select all

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

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

Posted: 05 Aug 2014 12:09
by binocular222
easyway:

Code: Select all

	$input = input("Enter a numerical character");
  if($input == 0  OR  $input * 1 != 0) {echo Number}; Else {echo Text}

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

Posted: 05 Aug 2014 13:11
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!!!!!!

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

Posted: 05 Aug 2014 22:59
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?

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

Posted: 06 Aug 2014 07:43
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.

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

Posted: 06 Aug 2014 11:16
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: )