Jump to content
hard5c0p3k1ng

Making script to test if inputted text is a number not text

Recommended Posts

_moni=_this select 0;
_good=false;
_max=true;
{
    if(_x==0) or (_x==1) or (_x==2) or (_x==3) or (_x==4) or (_x==5) or (_x==6) or (_x==7) or (_x==8) or (_x==9) then
    {
    }
    else
    {
    _max=false;
    };

}forEach _moni;

        if(_max)then
        {
            _good=true;
        }
        else
        {
            _good=false;
        };    

    if(_good)then
    {
            if (!isNil "life_no_injection") then {
                life_no_injection = true;
            };
            life_cash = life_cash + _moni;
            if (!isNil "life_no_injection") then {
                life_no_injection = false;
            };
        Hint format ["$%1 has been added to your inventory", (_moni)];
    }
    else
    {
        Hint format ["%1 is not valid", (_moni)];
    };
 

 

 

Calling the code with this:

 

//assume ctrlText 13 is valid (it is I tested it, the sqf calls the value perfectly fine

class myClass: RscButton
{
    idc = 1;
    action = "_nil=[ctrlText 13]ExecVM ""money.sqf""";

    text = "OK"; //--- ToDo: Localize;
    x = 0.402031 * safezoneW + safezoneX;
    y = 0.214017 * safezoneH + safezoneY;
    w = 0.04125 * safezoneW;
    h = 0.0549967 * safezoneH;
    colorBackground[] = {-1,-1,-1,0.3};
};

 

It has to be a simple mistake, I know c++ a form of this works (string is an array of chars) but idk if it is valid for arma 3.

Share this post


Link to post
Share on other sites

Didn't get further but please change

 if(_x==0) or (_x==1) or (_x==2) or (_x==3) or (_x==4) or (_x==5) or (_x==6) or (_x==7) or (_x==8) or (_x==9) then

to

if (_x in [1,2,3,4,5,6,7,8,9])

also, something like

str(parseNumber _text) == _text

should do it all.

Share this post


Link to post
Share on other sites
fnc_isStringNumberValid = 
{
	if (!(_this isEqualType "") || _this isEqualTo "") exitWith {false};
	{
		if (!(_x isEqualTo "0") && parseNumber _x isEqualTo 0) exitWith {true};
	} 
	count (_this splitString "") isEqualTo 0;
};

To test
 

hint str ("123" call fnc_isStringNumberValid); //true
hint str ("123a" call fnc_isStringNumberValid); //false

 

Edited by killzone_kid
added check for "", corrected more
  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, killzone_kid said:

*code*

Any reason not to use simply str(parseNumber _textNumber) == _textNumber as a condition?

Edit: Ah, I see, it could be empty.

Share this post


Link to post
Share on other sites
9 minutes ago, killzone_kid said:

parsenumber returns 0 if not number

That doesn't matter at all. Still, as I said, it could be empty.

 

Btw, I just tested it in debug console and

text1 = "51656"; hint str (str(parseNumber text1) == text1)

text1 = "155"; hint str (str(parseNumber text1) == text1)

text1 = "8852"; hint str (str(parseNumber text1) == text1)

...etc. all return true as expected but numbers over 1 000 000 don't.

Share this post


Link to post
Share on other sites
1 minute ago, theend3r said:

That doesn't matter at all. Still, as I said, it could be empty.


What doesn't matter? "0" would return false as well as "abc" if you do not test for "0"

.  

  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, killzone_kid said:


What doesn't matter? "0" would return false as well as "abc" if you do not test for "0"

.  

Testing for 0 returns true, abc false, as it should.

 

Edit: parseNumber switches to scientific format when over 999999, so my hack doesn't work while your solution does. :/

Share this post


Link to post
Share on other sites
8 minutes ago, theend3r said:

Testing for 0 returns true.


Yeah sorry I missed that you comparing str to str. Well in this case my solution is faster as str is a bit expensive. 

EDIT could you also delete quote with my code as it was not correct

Edited by killzone_kid

Share this post


Link to post
Share on other sites
9 minutes ago, theend3r said:

Testing for 0 returns true, abc false, as it should.

 

Edit: parseNumber switches to scientific format when over 999999, so my hack doesn't work while your solution does. :/

 

I was assuming it compares 1 digit at a time. Anyway could you delete quote with my incorrect code please

Share this post


Link to post
Share on other sites
49 minutes ago, killzone_kid said:

 

I was assuming it compares 1 digit at a time. Anyway could you delete quote with my incorrect code please

Done. And you're right, it's faster. I never would've guessed that going one digit at a time would be faster than comparing all at once but str seems really slow.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×