Best way to detect if players entered text is only digits?
#1

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.
Reply
#2

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.
Reply
#3

PHP код:
stock isNumeric(string[])
{
    for (new 
0strlen(string); ji++)
    {
        if (
string[i] > '9' || string[i] < '0') return 0;
    }
    return 
1;

Reply
#4

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
Reply
#5

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.
Reply
#6

This would work, if you're using sscanf
Код:
if (sscanf(string, "{i}")) // string is not all numbers
Reply
#7

What about strval?

PHP код:
if(strval(string))) 
Reply
#8

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.
Reply
#9

PHP код:
IsPinCode(input[])
{
    new 
len strlen(input);
    if(
len != 4) return 0;
    for(new 
0len++) if(input[i] < '0' || input[i] > '9') return 0;
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)