check is mail vaild
#1

hi guys so i have this code :
Код:
stock IsValidMailAddr(const addr[])
{
    new len = strlen( addr ),
        atcount = ( 0 ), IsValid = false
    ;
    if( len < 5 ){
        IsValid = false;
        goto IsValidMailAddr__the_end;
    }
    for(new i, j=len; i<j; 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] == '.' )
            IsValid = false;
    }
    IsValidMailAddr__the_end:
    return IsValid ? true : false;
}
(by Shinja)

so i want to it just support this providers : yahoo , gmail , hotmail and just this : .com , .us , .org can you please "edit" this code for me ?
Reply
#2

I'd rather use sscanf than editing it, especially since it doesn't even check for valid domains at all (so editing the code to check for specific domains would require more work than just solving it in a simpler way).

I'd just use sscanf to split the mail into 2 parts:

- email
- domain (gmail.com, yahoo.us, etc)

Код:
new email[65], domain[15];
sscanf(string, "s[65]'@'s[15]'", email, domain);
Now you can check domain for the desired content, eg. using strcmp.

Note that this doesn't ensure actually valid email addresses. But this shouldn't even matter because it'll be the user's fault if they cannot properly enter their email address. Or putting in fake emails (which is what everyone does if they don't want to give their real email anyway - so there's not really a point in a 100% email validation).
So if you use the email for activation purposes you can just send it to whatever the user typed it, it just won't get sent if it's wrong.

You could also use both if you really want the emails to be correct in format, by first using your function and then checking the domain only.
Reply
#3

Quote:
Originally Posted by NaS
Посмотреть сообщение
I'd rather use sscanf than editing it, especially since it doesn't even check for valid domains at all (so editing the code to check for specific domains would require more work than just solving it in a simpler way).

I'd just use sscanf to split the mail into 2 parts:

- email
- domain (gmail.com, yahoo.us, etc)

Код:
new email[65], domain[15];
sscanf(string, "s[65]'@'s[15]'", email, domain);
Now you can check domain for the desired content, eg. using strcmp.

Note that this doesn't ensure actually valid email addresses. But this shouldn't even matter because it'll be the user's fault if they cannot properly enter their email address. Or putting in fake emails (which is what everyone does if they don't want to give their real email anyway - so there's not really a point in a 100% email validation).
So if you use the email for activation purposes you can just send it to whatever the user typed it, it just won't get sent if it's wrong.

You could also use both if you really want the emails to be correct in format, by first using your function and then checking the domain only.
thnx for your reply
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)