15.11.2009, 19:16
IsValidEMail
Features
- Checks if the eMail is in correct format "localpart@domain.tld"
- Characters that are allowed for local part (username) are uppercase and lowercase letters, numbers, dot (.), underscore (_) and hyphen (-)
- You can't use dot (.) at the start and at the end of the local part, and you can't use double dots in local part(..)
- Local part has to contain one or more characters
- Charaters that are allowed for the domain name are only lowercase letters and hyphen (-)
- Domain name has to contain 3 or more characters
- Top Level Domains must match the ones from the TopLevelDomains list
Function
pawn Код:
stock IsValidEMail(email[])
{
new lenght = strlen(email),
emailsplit[2][MAX_PLAYER_EMAIL],
domainsplit[2][MAX_PLAYER_EMAIL],
atsign;
split(email, emailsplit, '@');
if (strlen(emailsplit[0]) > 0)
{
// Local part (username) is not empty. Continue...
}
else { return 0; } // No need to continue...
split(emailsplit[1], domainsplit, '.');
if (strlen(domainsplit[0]) > 2)
{
// Domain name has 3 or more charaters. Continue...
}
else { return 0; } // No need to continue...
new len = strlen(emailsplit[0]),
doubledot = strfind(emailsplit[0], "..", true);
if ((emailsplit[0][0] == '.') || (emailsplit[0][len - 1] == '.') || (doubledot != -1))
{
return 0; // The dot (.) can't be on the start or on the end of the local part (username) and double dot (..) is not allowed. Return false...
}
for (new i = 0; i < lenght; i++)
{
if (emailsplit[0][i])
{
if ((emailsplit[0][i] >= 'A' && emailsplit[0][i] <= 'Z') || (emailsplit[0][i] >= 'a' && emailsplit[0][i] <= 'z') || (emailsplit[0][i] >= '0' && emailsplit[0][i] <= '9') || (emailsplit[0][i] == '.') || (emailsplit[0][i] == '-') || (emailsplit[0][i] == '_'))
{
// Local part contains characters that are allowed. Continue...
}
else { return 0; } // No need to continue...
}
if (domainsplit[0][i])
{
if ((domainsplit[0][i] >= 'a' && domainsplit[0][i] <= 'z') || (domainsplit[0][i] == '-'))
{
// Domain name contains charaters that are allowed. Continue...
}
else { return 0; } // No need to continue...
}
// This checks that '@' sign is used only one time...
if (email[i] == '@')
{
atsign++;
if (atsign > 1) { return 0; } // No need to continue...
}
}
for (new i = 0; i < MAX_TOPLEVEL_DOMAINS; i++)
{
if (strcmp(domainsplit[1], TopLevelDomains[i], true) == 0)
{
return 1; // All checks are Ok, eMail is valid...
}
}
return 0; // If the inserted Top Level Domain don't match with the TopLevelDomains then return false...
}
Requirements
- Split Function
- Top Level Domain List
- Some Defines
pawn Код:
forward split(const strsrc[], strdest[][], delimiter);
public split(const strsrc[], strdest[][], delimiter)
{
new i, li;
new aNum;
new len;
while (i <= strlen(strsrc))
{
if (strsrc[i] == delimiter || i == strlen(strsrc))
{
len = strmid(strdest[aNum], strsrc, li, i, 128);
strdest[aNum][len] = 0;
li = i+1;
aNum++;
}
i++;
}
return 1;
}
pawn Код:
new TopLevelDomains[MAX_TOPLEVEL_DOMAINS][] = {
{"ac"},
{"ad"},
{"ae"},
{"aero"},
{"af"},
{"ag"},
{"ai"},
{"al"},
{"am"},
{"an"},
{"ao"},
{"aq"},
{"ar"},
{"arpa"},
{"as"},
{"asia"},
{"at"},
{"au"},
{"aw"},
{"ax"},
{"az"},
{"ba"},
{"bb"},
{"bd"},
{"be"},
{"bf"},
{"bg"},
{"bh"},
{"bi"},
{"biz"},
{"bj"},
{"bm"},
{"bn"},
{"bo"},
{"br"},
{"bs"},
{"bt"},
{"bv"},
{"bw"},
{"by"},
{"bz"},
{"ca"},
{"cat"},
{"cc"},
{"cd"},
{"cf"},
{"cg"},
{"ch"},
{"ci"},
{"ck"},
{"cl"},
{"cm"},
{"cn"},
{"co"},
{"com"},
{"coop"},
{"cr"},
{"cu"},
{"cv"},
{"cx"},
{"cy"},
{"cz"},
{"de"},
{"dj"},
{"dk"},
{"dm"},
{"do"},
{"dz"},
{"ec"},
{"edu"},
{"ee"},
{"eg"},
{"er"},
{"es"},
{"et"},
{"eu"},
{"fi"},
{"fj"},
{"fk"},
{"fm"},
{"fo"},
{"fr"},
{"ga"},
{"gb"},
{"gd"},
{"ge"},
{"gf"},
{"gg"},
{"gh"},
{"gi"},
{"gl"},
{"gm"},
{"gn"},
{"gov"},
{"gp"},
{"gq"},
{"gr"},
{"gs"},
{"gt"},
{"gu"},
{"gw"},
{"gy"},
{"hk"},
{"hm"},
{"hn"},
{"hr"},
{"ht"},
{"hu"},
{"id"},
{"ie"},
{"il"},
{"im"},
{"in"},
{"info"},
{"int"},
{"io"},
{"iq"},
{"ir"},
{"is"},
{"it"},
{"je"},
{"jm"},
{"jo"},
{"jobs"},
{"jp"},
{"ke"},
{"kg"},
{"kh"},
{"ki"},
{"km"},
{"kn"},
{"kp"},
{"kr"},
{"kw"},
{"ky"},
{"kz"},
{"la"},
{"lb"},
{"lc"},
{"li"},
{"lk"},
{"lr"},
{"ls"},
{"lt"},
{"lu"},
{"lv"},
{"ly"},
{"ma"},
{"mc"},
{"md"},
{"me"},
{"mg"},
{"mh"},
{"mil"},
{"mk"},
{"ml"},
{"mm"},
{"mn"},
{"mo"},
{"mobi"},
{"mp"},
{"mq"},
{"mr"},
{"ms"},
{"mt"},
{"mu"},
{"museum"},
{"mv"},
{"mw"},
{"mx"},
{"my"},
{"mz"},
{"na"},
{"name"},
{"nc"},
{"ne"},
{"net"},
{"nf"},
{"ng"},
{"ni"},
{"nl"},
{"no"},
{"np"},
{"nr"},
{"nu"},
{"nz"},
{"om"},
{"org"},
{"pa"},
{"pe"},
{"pf"},
{"pg"},
{"ph"},
{"pk"},
{"pl"},
{"pm"},
{"pn"},
{"pr"},
{"pro"},
{"ps"},
{"pt"},
{"pw"},
{"py"},
{"qa"},
{"re"},
{"ro"},
{"rs"},
{"ru"},
{"rw"},
{"sa"},
{"sb"},
{"sc"},
{"sd"},
{"se"},
{"sg"},
{"sh"},
{"si"},
{"sj"},
{"sk"},
{"sl"},
{"sm"},
{"sn"},
{"so"},
{"sr"},
{"st"},
{"su"},
{"sv"},
{"sy"},
{"sz"},
{"tc"},
{"td"},
{"tel"},
{"tf"},
{"tg"},
{"th"},
{"tj"},
{"tk"},
{"tl"},
{"tm"},
{"tn"},
{"to"},
{"tp"},
{"tr"},
{"travel"},
{"tt"},
{"tv"},
{"tw"},
{"tz"},
{"ua"},
{"ug"},
{"uk"},
{"us"},
{"uy"},
{"uz"},
{"va"},
{"vc"},
{"ve"},
{"vg"},
{"vi"},
{"vn"},
{"vu"},
{"wf"},
{"ws"},
{"ye"},
{"yt"},
{"yu"},
{"za"},
{"zm"},
{"zw"}
};
pawn Код:
#define MAX_PLAYER_EMAIL 128
#define MAX_TOPLEVEL_DOMAINS 269
- Wikipedia for eMail Address Info - http://en.wikipedia.org/wiki/E-mail_address
- ****** for TLD link - http://data.iana.org/TLD/tlds-alpha-by-domain.txt