Best way to detect if players entered text is only digits? -
Type-R - 01.08.2016
Hello, well i am making a banking system and i want to make it so, that you have to create an account with a 4-digit pin code. I tried googling and searching around the website, but i could not find what is the best way to detect players input text if it only contains digits?? Thank you.
Re: Best way to detect if players entered text is only digits? -
PrO.GameR - 01.08.2016
Search for isnumeric function..
PS: most of includes do use that, so just give it a try, I'm pretty sure if you are using sscanf it does have that.
Re: Best way to detect if players entered text is only digits? -
Shinja - 01.08.2016
PHP код:
stock isNumeric(string[])
{
for (new i = 0, j = strlen(string); i < j; i++)
{
if (string[i] > '9' || string[i] < '0') return 0;
}
return 1;
}
Re: Best way to detect if players entered text is only digits? -
Misiur - 01.08.2016
If you are using sscanf, then it's just
pawn Код:
if(sscanf(yourstring, "{dddd}")) //fail
Otherwise, something like
pawn Код:
for (new i = 0; i != MAX_PIN_DIGITS; ++i) if(!('0' <= input[i] <= '9')) //fail
//Beaten by fast typers
Re: Best way to detect if players entered text is only digits? -
Vince - 01.08.2016
Quote:
Originally Posted by Misiur
If you are using sscanf, then it's just
pawn Код:
if(sscanf(yourstring, "{dddd}")) //fail
|
That won't work because those numbers then need to be separated by spaces and in addition can be longer than one character.
Re: Best way to detect if players entered text is only digits? -
Stinged - 01.08.2016
This would work, if you're using sscanf
Код:
if (sscanf(string, "{i}")) // string is not all numbers
Re: Best way to detect if players entered text is only digits? -
Luicy. - 01.08.2016
What about strval?
PHP код:
if(strval(string)))
Re: Best way to detect if players entered text is only digits? -
Stinged - 01.08.2016
Quote:
Originally Posted by Meller
What about strval?
PHP код:
if(strval(string)))
|
Wouldn't work. It returns 0 when the string is not a number.
But it also returns 0 when the integer is equal to zero.
That would deny '0000' as a 4 digit pin code.
Re: Best way to detect if players entered text is only digits? -
NaS - 02.08.2016
PHP код:
IsPinCode(input[])
{
new len = strlen(input);
if(len != 4) return 0;
for(new i = 0; i < len; i ++) if(input[i] < '0' || input[i] > '9') return 0;
return 1;
}