我在我的发言中做了类似的事情,我证实,电子邮件地址现场有两部分用“@文号分开,至少有两部分用文号分开。 这并不阻止它是一个有效的电子邮件地址,但确实确保它至少是正确的格式。 守则例:
// to validate email address, just checks for @ and . separators
NSArray *validateAtSymbol = [[emailRegisterTextField text] componentsSeparatedByString:@"@"];
NSArray *validateDotSymbol = [[emailRegisterTextField text] componentsSeparatedByString:@"."];
// checks to make sure entries are good (email valid, username available, passwords enough chars, passwords match
if ([passwordRegisterTextField text].length >= 8 &&
[passwordRegisterTextField text].length > 0 &&
[[passwordRegisterTextField text] isEqual:[passwordVerifyRegisterTextField text]] &&
![currentUser.userExist boolValue] &&
![[emailRegisterTextField text] isEqualToString:@""] &&
([validateAtSymbol count] == 2) &&
([validateDotSymbol count] >= 2)) {
// get user input
NSString *inputEmail = [emailRegisterTextField text];
NSString *inputUsername = [userNameRegisterTextField text];
NSString *inputPassword = [passwordRegisterTextField text];
NSString *inputPasswordVerify = [passwordVerifyRegisterTextField text];
NSLog(@"inputEmail: %@",inputEmail);
NSLog(@"inputUsername: %@",inputUsername);
NSLog(@"inputPassword: %@",inputPassword);
NSLog(@"inputPasswordVerify: %@",inputPasswordVerify);
// attempt create
[currentUser createUser:inputEmail username:inputUsername password:inputPassword passwordVerify:inputPasswordVerify];
}
else {
NSLog(@"error");
[errorLabel setText:@"Invalid entry, please recheck"];
}
如果情况不正确,你可以发出警示,但我选择用错误信息显示<条码>:Label/code>,因为对使用者来说,这种警示似乎不太jar。 在上述法典中,我检查了电子邮件地址的格式、密码长度以及密码(两次送交核查)。 如果所有测试都未能通过,则评估者没有采取行动。 你可以选择你想要证实的哪一个领域。