Just My Life & My Work

Archive for 2016 年 11 月 18 日

[iOS] 檢查Email格式 (Check Email Format)

製作跟會員有關係的App,很常需要檢查Email格式 (Check Email Format),儘管知道這跟正規語言(Regular Language)有關係,但還是請高手幫我寫規則吧XD~

/**
 Theme: Check Email Format
 IDE: Xcode 8
 Language: Objective C
 Date: 105/11/14
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */

-(BOOL)isValidEmail:(NSString *)checkString
{
    BOOL stricterFilter = NO; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
    NSString *stricterFilterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
    NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}

以後直接來這裡複製貼上吧~

參考:Check that an email address is valid on iOS

標籤雲

%d 位部落客按了讚: