So, habe mal mein bestes versucht, eine Funktion zu schreiben, welche das Email-Format
C
new LegalSpecialChars[] = {'!','#','$','%','&','a','m','p',';','\'','*','+','-','/','=','?','^','_','`','.','{','|','}','~'};
new ValidDomains[][10] = {".de",".com",".net",".berlin"};
stock IsValidEmail(String[])
{
new bool:valid = true,
bool:validDomain = false;
if(strfind(String, "@", false, 0) >= 1 && strlen(String) > strfind(String, "@", false, 0) + 1) {
new part[2][64];
split(String, part, '@');
// vor dem @
for(new i=0; i<strlen(part[0]); i++){
new bool:validChar = true;
if(!((part[0][i] >= 0x41 && part[0][i] <= 0x5A) || (part[0][i] >= 0x61 && part[0][i] <= 0x7A))) validChar = false;
if(!validChar) {for(new c=0; c<sizeof(LegalSpecialChars); c++){if(part[0][i] == LegalSpecialChars[c]) {validChar = true;break;}}}
if(!validChar) {valid = false;break;}
}
if(!valid) goto IsValidEmail_END;
// nach dem @
for(new i=0; i<strlen(part[1]); i++){
new bool:validChar = true;
if(!((part[1][i] >= 0x41 && part[1][i] <= 0x5A) || (part[1][i] >= 0x61 && part[1][i] <= 0x7A) || part[1][i] == 0x2D || part[1][i] == 0x2E)) validChar = false;
if(!validChar) {valid = false;break;}
}
if(!valid) goto IsValidEmail_END;
// domain check
for(new i=0; i<sizeof(ValidDomains); i++) {
if(strfind(String, ValidDomains[i], false, 0) == strlen(String) - strlen(ValidDomains[i])) {validDomain = true;break;}
}
if(!validDomain) {valid = false; goto IsValidEmail_END;}
}
IsValidEmail_END:
printf("%s = %b", String, valid);
return valid;
}
Alles anzeigen
Output / Test:
C
public OnGameModeInit()
{
print("----- Valide Emailadressen -----");
IsValidEmail("heinrich@hotmail.de");
IsValidEmail("heinrich@hotmail.com");
IsValidEmail("heinrich@hotmail.net");
IsValidEmail("heinrich@hotmail.berlin");
print("----- Invalid wegen unerlaubten Domain-Endungen -----");
IsValidEmail("heinrich@hotmail.tv");
IsValidEmail("heinrich@hotmail.us");
IsValidEmail("heinrich@hotmail.cc");
print("----- Invalid wegen falscher Zeichensetzung -----");
IsValidEmail("heinrich@.cchotmail");
IsValidEmail("heinrich@hotmail,cc");
IsValidEmail("heinrich@hotmail:cc");
IsValidEmail("heinrich@ho_tmail.cc");
IsValidEmail("heöinrich@hotmail.de");
/*
Output:
[21:44:54] ----- Valide Emailadressen -----
[21:44:54] heinrich@hotmail.de = 1
[21:44:54] heinrich@hotmail.com = 1
[21:44:54] heinrich@hotmail.net = 1
[21:44:54] heinrich@hotmail.berlin = 1
[21:44:54] ----- Invalid wegen unerlaubten Domain-Endungen -----
[21:44:54] heinrich@hotmail.tv = 0
[21:44:54] heinrich@hotmail.us = 0
[21:44:54] heinrich@hotmail.cc = 0
[21:44:54] ----- Invalid wegen falscher Zeichensetzung -----
[21:44:54] heinrich@.cchotmail = 0
[21:44:54] heinrich@hotmail,cc = 0
[21:44:54] heinrich@hotmail:cc = 0
[21:44:54] heinrich@ho_tmail.cc = 0
[21:44:54] heöinrich@hotmail.de = 0
*/
}
Alles anzeigen
//EDIT:
Achja, hab vergessen: Es wird noch die Split-Funktion aus der SAMP-Wiki verwendet:
C
//Faster (new) split Code [[User:Kaliber|Kaliber]]
stock split(const src[], dest[][], const delimiter)
{
new n_pos,num,old,str[1];
str[0] = delimiter;
while(n_pos != -1)
{
n_pos = strfind(src,str,false,n_pos+1);
strmid(dest[num++], src, (!num)?0:old+1,(n_pos==-1)?strlen(src):n_pos,256);
old=n_pos;
}
return 1;
}
Alles anzeigen