AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Only Numbers (https://forums.alliedmods.net/showthread.php?t=330777)

Dragos 02-20-2021 15:05

Only Numbers
 
Hello,

I'm pretty sure it was a function like this.

So, It check If the entered words contain only numbers, no words

PHP Code:

new szArg[192];
    
read_argsszArgcharsmaxszArg ) );
    
remove_quotesszArg );
    new 
ids str_to_num(szArg)
    
    if(
ids == 0) {
        
culoare(id"!g[!tSYSTEM!g] !tWe Accept Only Numbers")
        return 
PLUGIN_HANDLED;
    } 

If I write like 123456 it block me
But If I type 123456A, it allow him and pass next

Bugsy 02-20-2021 15:33

Re: Only Numbers
 
Use is_str_num()

Natsheh 02-20-2021 16:29

Re: Only Numbers
 
PHP Code:


stock bStringAreNumbers
(const String[])
{
   for(new 
imaxloop strlen(String); maxloopi++)
   {
      if( ! ( 
'0' <= String] <= '9' ) ) return false;
   }
   return 
true;



although listen for what bugsy told you, this example is just for clarification!

Dragos 02-20-2021 18:21

Re: Only Numbers
 
My bad, I wanted to say that If I type 123456 [Need to type regular numbers to pass] it let me go, but If I type 123456A it still pass me <-- [And this is what I want to block] [What I want to block it's that , If I type some numbers and in that numbers contain a words is should return ]

Dragos 02-20-2021 18:43

Re: Only Numbers
 
It lock like doing it negative it solved my problem

PHP Code:

if(!bStringAreNumbers(szArg)) {
        
culoare(id"!g[!tSYSTEM!g] !tWe Accept Only Numbers");
        return 
PLUGIN_HANDLED;
    } 

Ty

loiraolhosazul 02-20-2021 21:51

Re: Only Numbers
 
Or:

PHP Code:

stock bool:xValidNumber(const xStr[])
{
    new const 
xNumbers[][] =
    {
        
"0""1""2""3""4""5""6""7""8""9"
    
}
    
    new 
xLen strlen(xStr)
    new 
iaxValids
    
    
for(0xLeni++)
    {
        for(
0sizeof(xNumbers); a++)
        {
            if(
xStr[i] == xNumbers[a][0])
            {
                
xValids ++
                break
            }
        }
    }
    
    if (
xValids != xLen)
        return 
false
    
    
return true



Bugsy 02-20-2021 23:06

Re: Only Numbers
 
Beating this one to death guys..I'd just use the native is_str_num() and call it a day. Natsheh's function is better/more efficient than what you posted, loiraolhosazul.


All times are GMT -4. The time now is 16:16.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.