Function "IsValidMailAddr" is doing weird
#1

Hi,

I've just made an "IsValidMailAddr" function, but it isn't working how I want it to work.
It's only working one time... ?
Here's the function

pawn Код:
stock IsValidMailAddr(addr[])
{
    new l = 0,
        atcount, bool:IsValid = false
    ;
   
    l = strlen(addr);
   
    loop:l(i){
        if(addr[i] == '@') atcount++;
       
        if( ( (addr[i] >= 'a' && addr[i] <= 'z') || (addr[i] >= 'A' && addr[i] <= 'Z') || (addr[i] == '-') || (addr[i] == '.') ) && atcount == 1 )
            IsValid = true;
        else{
            IsValid = false;
            break;
        }

        if(i == l-1){
            if(!((addr[i] >= 'a' && addr[i] <= 'z') || (addr[i] >= 'A' && addr[i] <= 'Z'))){
                IsValid = false;
                break;
            }
        }
    }
    return IsValid ? true : false;
}
And the test in OnGameModeInit:
pawn Код:
if(IsValidMailAddr("kwarde@rpdsamp.nl")) print("MAIL OK"); else print("MAIL NOT OK"); //MAIL
if(IsValidMailAddr("kwarde@@rpdsamp.nl")) print("MAIL 2 OK"); else print("MAIL 2 NOT OK"); //MAIL 2
if(IsValidMailAddr("kwarde@rpdsamp.nl.")) print("MAIL 3 OK"); else print("MAIL 3 NOT OK"); //MAIL 3
if(IsValidMailAddr("kwarde@rpdsamp.nl")) print("MAIL 4 OK"); else print("MAIL 4 NOT OK"); //MAIL 4
if(IsValidMailAddr("k,warde@rpdsamp.nl")) print("MAIL 5 OK"); else print("MAIL 5 NOT OK"); //MAIL 5
As you can see, "Mail 4" is the same as "Mail". But this is the output in the console:
Код:
MAIL OK
MAIL 2 NOT OK
MAIL 3 NOT OK
MAIL 4 NOT OK
MAIL 5 NOT OK
It says from mail 4 that it isn't OK, while MAIL is OK.

Thanks if you can help me.

- Kevin
Reply
#2

I do not have the answer to your exact problem but I tried building my own IsValidEmail modeled after yours and tested it using your 4 emails. You could probably compare my function with yours and find out the problem.

Function:
pawn Код:
stock IsValidEmail(email[])
{
    new a_count, length = strlen(email);
    if(length < 5)
        return false;
    for(new i = 0; i < strlen(email); i++)
    {
        if(email[i] == '@')
        {
            a_count++;
            if(a_count > 1)
            {
                return false;
            }
        }
        if( (email[i] < 'A' && email[i] > 'Z') || (email[i] < 'a' && email[i] > 'z') )
        {
            if(email[i] != '.')
            {
                return false;
            }
        }
        if(i == length-1 && email[i] == '.')
            return false;
    }
    return true;
}
Test code:
pawn Код:
printf("%d", IsValidEmail("kwarde@rpdsamp.nl"));
printf("%d", IsValidEmail("kwarde@rpdsamp.nl."));
printf("%d", IsValidEmail("kwa*de@@rpdsamp.nl"));
printf("%d", IsValidEmail("kwarde@rpdsamp.nl"));
Test Results:
1 = true
0 = false
Код:
1
0
0
1
Reply
#3

Thanks I'll edit it, but I'm still wondering why mine didn't work properly.
By the way, yours wasn't 100% good

pawn Код:
if(IsValidMail("k#vin@rpdsamp.nl")) print("MAIL OK"); else print("MAIL NOT OK");
The "#" is invalid, this is the output.

Код:
MAIL OK


However,
Can anyone tell me why mine didn't work properly, why it'd output the first time OK, and the second time NOT OK, while it was the same :S

- Kevin
Reply
#4

I would put print statements anywhere IsValid is set to true or false, and then check the code and see where it goes wrong.
Reply
#5

Debugging ^^
But ehm, the first time it said 'OK' and the second time, with the same adress 'NOT OK', so I don't think I'll find it in the code
Reply
#6

EDIT: Sorry, I forgot the EDIT button 'cuz I'm in a hurry. I gotta go now

It's working now
pawn Код:
stock IsValidMailAddr(const addr[])
{
    new len = strlen( addr ),
        atcount = ( 0 ), IsValid = false
    ;
    if( len < 5 ){
        IsValid = false;
        goto IsValidMailAddr__the_end;
    }
    loop:len(i){
        if( addr[i] == '@' ) atcount ++;
        if( atcount > 1 ){
            IsValid = false;
            break;
        }
       
        if( ( addr[i] >= 'a' && addr[i] <= 'z' ) || ( addr[i] >= 'A' && addr[i] <= 'Z' ) || ( addr[i] == '.' ) || ( addr[i] == '_' ) || ( addr[i] == '-' ) )
            IsValid = true;
        else{
            if( ( addr[i] != '@' ) ){
                IsValid = false;
                break;
            }
        }

        if( i + 1 == len )
            if( ( ( addr[i] >= 'a' && addr[i] <= 'z' ) || ( addr[i] >= 'A' && addr[i] <= 'Z' ) ) && ( addr[i] != '.' ) )
                IsValid = true;
        if( i + 1 == len && addr[i] == '.' ) //unneeded? :P
            IsValid = false;
    }
    IsValidMailAddr__the_end:
    return IsValid ? true : false;
}
The test:

pawn Код:
if(IsValidMailAddr("kwarde@rpdsamp.nl")) print("MAIL OK"); else print("MAIL NOT OK"); //MAIL
if(IsValidMailAddr("kwarde@@rpdsamp.nl")) print("MAIL 2 OK"); else print("MAIL 2 NOT OK"); //MAIL 2
if(IsValidMailAddr("kwarde@rpdsamp.nl.")) print("MAIL 3 OK"); else print("MAIL 3 NOT OK"); //MAIL 3
if(IsValidMailAddr("kwarde@rpdsamp.nl")) print("MAIL 4 OK"); else print("MAIL 4 NOT OK"); //MAIL 4
if(IsValidMailAddr("k,warde@rpdsamp.nl")) print("MAIL 5 OK"); else print("MAIL 5 NOT OK"); //MAIL 5
The output:

Код:
MAIL OK
MAIL 2 NOT OK
MAIL 3 NOT OK
MAIL 4 OK
MAIL 5 NOT OK
- Kevin
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)