iphone - 有没有办法让文本字段条目必须是电子邮件? (在 xcode 中)

标签 iphone mysql objective-c xcode ios4

我想制作一个用户登录表单,它需要使用电子邮件而不仅仅是用户名。如果不是电子邮件,有什么方法可以弹出警报吗?顺便说一句,所有这些都在 xcode 中。

最佳答案

有一种方法可以使用 NSPredicateregular expression :

- (BOOL)validateEmail:(NSString *)emailStr {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr];
}

然后,如果电子邮件地址错误,您可以显示警报:

- (void)checkEmailAndDisplayAlert {
    if(![self validateEmail:[aTextField text]]) {
        // user entered invalid email address
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    } else {
        // user entered valid email address
    }
}

关于iphone - 有没有办法让文本字段条目必须是电子邮件? (在 xcode 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123667/

相关文章:

iphone - iOS:是否允许使用libIOKit.dylib

iphone - sizeWithFont 之间的差异 :constrainedToSize:lineBreakMode: and textView. contentSize.height

iphone - CGContextDrawImage 返回错误的访问

iphone - ios 我如何比较 nsmutablearray 中的 nsdictionary

mysql - SQL 查询 - 显示所有可用和不可用的 ChairNo

mysql - xtradb vs innodb

php - 如何使用某些联接或其他查询检索所需的数据/结果?

ios - GoogleService-info.plist 中的 Build Settings User-Defined Setting

ios - 在 UIScrollVIew 中确定 UITextView 的滚动偏移量

ios - 在 Objective-C 中声明没有确定大小的数组