SHEmailValidator是一个iOS库,它会提供最基本的电子邮件语法检查,然后给出可能的修改建议(比如说,
test@gamil.con会被修改成
test@gmail.com)。
基本的语法检测
NSError *error = nil;
[[[SHEmailValidator] validator] validateSyntaxOfEmailAddress:emailAddress withError:&error];
if (error) {
// An error occurred
switch (error.code) {
case SHBlankAddressError:
// Input was empty
break;
case SHInvalidSyntaxError:
// Syntax completely wrong (probably missing @ or .)
break;
case SHInvalidUsernameError:
// Local portion of the email address is empty or contains invalid characters
break;
case SHInvalidDomainError:
// Domain portion of the email address is empty or contains invalid characters
break;
case SHInvalidTLDError:
// TLD portion of the email address is empty, contains invalid characters, or is under 2 characters long
break;
}
} else {
// Basic email syntax is correct
}
获取修改建议
NSError *error = nil;
NSString *suggestion = [[[SHEmailValidator] validator] autocorrectSuggestionForEmailAddress:emailAddress withError:&error];
if (error) {
// The syntax check failed, so no suggestions could be generated
} else if (suggestion) {
// A probable typo has been detected and the suggestion variable holds the suggested correction
} else {
// No typo was found, or no suggestions could be generated
}