www.ClassicTW.com
http://classictw.com/

Restricting User Input
http://classictw.com/viewtopic.php?f=15&t=34725
Page 1 of 2

Author:  Cruncher [ Sat Oct 17, 2015 11:17 pm ]
Post subject:  Restricting User Input

I have some fields that must have an alpha string value from the user.

I'm not seeing an operand "/=NULL" in TWX Proxy Script Reference.

I've tried if ( $var = 0 ) and if ( $var = "0" ) but that didn't work with a string.

Author:  Vid Kid [ Sun Oct 18, 2015 3:12 am ]
Post subject:  Re: Restricting User Input

Cruncher wrote:
I have some fields that must have an alpha string value from the user.

I'm not seeing an operand "/=NULL" in TWX Proxy Script Reference.

I've tried if ( $var = 0 ) and if ( $var = "0" ) but that didn't work with a string.


Try IF ($var = "")

Vid

Author:  Cruncher [ Sun Oct 18, 2015 7:50 am ]
Post subject:  Re: Restricting User Input

Vid Kid wrote:
Cruncher wrote:
I have some fields that must have an alpha string value from the user.

I'm not seeing an operand "/=NULL" in TWX Proxy Script Reference.

I've tried if ( $var = 0 ) and if ( $var = "0" ) but that didn't work with a string.


Try IF ($var = "")

Vid


That was the first thing I tried:

Code:
:LoopThis
getInput $var "* Question for Player"
getText $var $value "" ""
echo "**"& ANSI_14 " This is how you answered " & ANSI_10 $value "**"
   if ($value = " ")
      echo ANSI_15 " You must answer"
      goto :LoopThis
   end


This code is still accepting an empty string.

Author:  CBYNot [ Sun Oct 18, 2015 12:42 pm ]
Post subject:  Re: Restricting User Input

Cruncher wrote:
Vid Kid wrote:
Cruncher wrote:
I have some fields that must have an alpha string value from the user.

I'm not seeing an operand "/=NULL" in TWX Proxy Script Reference.

I've tried if ( $var = 0 ) and if ( $var = "0" ) but that didn't work with a string.


Try IF ($var = "")

Vid


That was the first thing I tried:

Code:
:LoopThis
getInput $var "* Question for Player"
getText $var $value "" ""
echo "**"& ANSI_14 " This is how you answered " & ANSI_10 $value "**"
   if ($value = " ")
      echo ANSI_15 " You must answer"
      goto :LoopThis
   end


This code is still accepting an empty string.


Looks like you've got a space in if ($value = " ") - try as:

if ($value = "")

Also if the input you are attempting to get is a number you can restrict entries using the $isnum command to verify that the input is a number before moving on, and an elseif to verify that # is not 0.

Author:  Cruncher [ Sun Oct 18, 2015 12:49 pm ]
Post subject:  Re: Restricting User Input

CBYNot wrote:
Cruncher wrote:

That was the first thing I tried:

Code:
:LoopThis
getInput $var "* Question for Player"
getText $var $value "" ""
echo "**"& ANSI_14 " This is how you answered " & ANSI_10 $value "**"
   if ($value = " ")
      echo ANSI_15 " You must answer"
      goto :LoopThis
   end


This code is still accepting an empty string.


Looks like you've got a space in if ($value = " ") - try as:

if ($value = "")

Also if the input you are attempting to get is a number you can restrict entries using the $isnum command to verify that the input is a number before moving on, and an elseif to verify that # is not 0.


Excellent, that did it for the strings. Silly little nuances of TWX that don't line up with what I've learned for C#.

I am using isNumber, and that's fine, except that it also accepts the number 0, which I don't want.

I suppose I could change that up and parse the planet number we're starting on, since I do a prompt check for Citadel.

Thanks again... back to work :)

Author:  Vid Kid [ Sun Oct 18, 2015 1:18 pm ]
Post subject:  Re: Restricting User Input

For numbers try :

:Top
GetInput $Value "* Number Question for Player"
IsNumber $YN $Value
IF ($YN = TRUE) and ($Value > "0")
Else
Got :Top
End


Vid

Author:  Cruncher [ Sun Oct 18, 2015 9:09 pm ]
Post subject:  Re: Restricting User Input

Vid Kid wrote:
For numbers try :

:Top
GetInput $Value "* Number Question for Player"
IsNumber $YN $Value
IF ($YN = TRUE) and ($Value > "0")
Else
Got :Top
End


Vid


Thanks Vid

Edit: Script compilation error: Unnecessary parameters after IF macro

I think I'll just parse the planet number, that makes more sense since you need to be in the citadel to start.

Author:  SteveH_66 [ Tue Oct 20, 2015 2:26 am ]
Post subject:  Re: Restricting User Input

Cruncher wrote:
Vid Kid wrote:
For numbers try :

:Top
GetInput $Value "* Number Question for Player"
IsNumber $YN $Value
IF ($YN = TRUE) and ($Value > "0")
Else
Got :Top
End


Vid


Thanks Vid

Edit: Script compilation error: Unnecessary parameters after IF macro

I think I'll just parse the planet number, that makes more sense since you need to be in the citadel to start.


I can't speak to the rest of what you all are talking about here, I would have to study the code further, and math isn't my forte. :lol: But if you ran Vid's code as it is in the quotes above, I think you might be getting that "Edit: Script compilation error: Unnecessary parameters after IF macro" error because of a typo. Vid or someone else should be able to confirm if I'm right, but I think "Got :Top" should read "Goto :Top"

Hope that helps, if I'm correct and this is a typo.

Edit : Also, while not having studied the code example Vid did for you, it seems like to me that there should be some kind of action to be taken that was left out of the code example between IF and Else. I think not having anything between IF and Else might throw an error code as well. Like :

Quote:
:Top
GetInput $Value "* Number Question for Player"
IsNumber $YN $Value
IF ($YN = TRUE) and ($Value > "0")
goto :startNextAction
Else
Goto :Top
End


My apologies Vid if this is not the case. Chalk it up to an amateur trying to debug your code lol

Author:  Vid Kid [ Tue Oct 20, 2015 12:46 pm ]
Post subject:  Re: Restricting User Input

Actually the typo was the Goto :Top

and you do not need anything between the IF else ..
It will fall through ..

I am going to check the Getinput .. I see an *
and do not like that .. so will check if that is ok ..

Yup , its ok to put in there .. so the only problem was the typo in the command goto.

Thanks for the catch.
2 heads are better then one.

Vid

Author:  SteveH_66 [ Tue Oct 20, 2015 2:03 pm ]
Post subject:  Re: Restricting User Input

I just get lucky once in a while Vid, if you ever saw me scripting you would laugh your head off. Pulling my hair out for half an hour before I find the typo or where I messed the math up lol.

I didn't know you could have an IF without an 'action' or 'statement' (don't know what programmers would call it) after it. Thanks for pointing that out, it makes it easier for someone to write a script with a check in it.

I am guessing that is the purpose of the script block Cruncher is working on.

Author:  Cruncher [ Wed Oct 21, 2015 10:13 pm ]
Post subject:  Re: Restricting User Input

Vid Kid wrote:
Actually the typo was the Goto :Top

and you do not need anything between the IF else ..
It will fall through ..

I am going to check the Getinput .. I see an *
and do not like that .. so will check if that is ok ..

Yup , its ok to put in there .. so the only problem was the typo in the command goto.

Thanks for the catch.
2 heads are better then one.

Vid


Yeah, it wasn't the goto loop it was the if statement:
IF ($YN = TRUE) and ($Value > "0")

Author:  Vid Kid [ Thu Oct 22, 2015 5:21 am ]
Post subject:  Re: Restricting User Input

Cruncher wrote:
Vid Kid wrote:
Actually the typo was the Goto :Top

and you do not need anything between the IF else ..
It will fall through ..

I am going to check the Getinput .. I see an *
and do not like that .. so will check if that is ok ..

Yup , its ok to put in there .. so the only problem was the typo in the command goto.

Thanks for the catch.
2 heads are better then one.

Vid


Yeah, it wasn't the goto loop it was the if statement:
IF ($YN = TRUE) and ($Value > "0")



That part is entirely correct , and works on Win 10.
If you wrote it like :

IF ($YN) and ($Value > "0")

Which works for XP and Win 7 and 8
but will not work with Win 10

Win 10 sees the ($YN) as a null string and fails.
I have tested scripts with Micro .. and almost every
time I had to change the ($YN) to ($YN = TRUE)
for them to work for his Win 10 box.

Vid

Author:  Cruncher [ Thu Oct 22, 2015 8:54 pm ]
Post subject:  Re: Restricting User Input

Cruncher wrote:

Yeah, it wasn't the goto loop it was the if statement:
IF ($YN = TRUE) and ($Value > "0")



Code:
              isNumber $test $value
Line 29>     if ($test = TRUE) and ($value >"0")
             echo ANSI_15 " oops, not a whole number.  Please enter "
             goto :Loopthis
         end


Script compilation error: Unnecessary parameters after IF macro, line 29

I'm using Win7

Author:  Kaus [ Thu Oct 22, 2015 10:05 pm ]
Post subject:  Re: Restricting User Input

Cruncher wrote:
Cruncher wrote:

Yeah, it wasn't the goto loop it was the if statement:
IF ($YN = TRUE) and ($Value > "0")



Code:
              isNumber $test $value
Line 29>     if ($test = TRUE) and ($value >"0")
             echo ANSI_15 " oops, not a whole number.  Please enter "
             goto :Loopthis
         end


Script compilation error: Unnecessary parameters after IF macro, line 29

I'm using Win7


space after >

Author:  ElderProphet [ Thu Oct 22, 2015 10:06 pm ]
Post subject:  Re: Restricting User Input

I can shed some light on this.

First thing, I think you need something to follow the IF test, before the ELSE... try throwing a comment in there.

Secondly, in many programming languages, logic tests are short-circuited... which is to say, if the first argument needs to be true, but isn't, then nothing else is tested. And you can extrapolate that short-circuit behavior on and on... if an argument fails its test, nothing further is tested... in many programming languages.

So, Vid's statement would be fine... except TWX scripts DO NOT short-circuit logic tests. So let's recap:
> isNumber $yn $number
> if ($yn =TRUE) AND ($value > 0)
So indeed, the first argument fails if it's not a number... but the second comparison will still be attempted, and throw an exception if $value isn't a number. Make sense? I tried to alter this behavior, but it was non-trivial, and I don't think I succeeded.

So, you have to nest any comparisons that require a number inside of the isNumber test:
Code:
isNumber $yn $number
if ($yn = TRUE)
  if ($value > 0) // now it's safe, we verified it
    echo "Mathematical!!"
  end
end


Cruncher, why wouldn't you want the number zero to pass the isNumber test? You may be creating the zero just by testing a value. Share more, and I'll elaborate if it applies.

+EP+

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/