2017-03-08 4 views
0

私は電子メールIDを検証する:正規表現

[email protected]: - 有効な

[email protected]:無効

私の知る限り、電子メールIDの "@"の後ろに複数のドットは使用できません。 @の後ろの1つの点をより多く検出し、検証を確認する方法を教えてください。

私は電子メールの検証に以下の正規表現を使用しています。 両方とも正常に動作しています。 しかし、@記号の後に複数のドットをチェックすることはできません。

NSString *emailRegex = 
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}" 
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\" 
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-" 
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5" 
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-" 
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21" 
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex]; 

    return [emailTest evaluateWithObject:email]; 

また、私はこれを試してみました:

NSString *regex1 = @"\\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,4}\\z"; 
    NSString *regex2 = @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*"; 
    NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1]; 
    NSPredicate *test2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2]; 
    return [test1 evaluateWithObject:email] && [test2 evaluateWithObject:email]; 
+2

なぜ複数のドットを使用できないのですか?ホスト名が多くの点を持つことは完全に有効です。あなたがそれらを制限しようとすると、英国のすべての会社(company.co.uk)のように、かなり怒っている人々がいるでしょう... –

+0

[電子メールアドレスがiOS上で有効であることを確認してください] /stackoverflow.com/questions/3139619/check-that-an-email-address-is-valid-on-ios) –

+0

[サインイン検証](http://www.programmingcrew.in/2015/09/sign)を確認してください。 -in-validations-in-objective-v.html) – pkc456

答えて

2

NSDataDetectorを使用し、このために正規表現を使用しないでください。この

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

     return [emailTest evaluateWithObject:email]; 
} 
+0

[email protected]には適用されません –

+0

はチェックに失敗しました@ –

3

を試してください:

NSError *error = nil; 
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error]; 
if (!detector) { /* handle error */ } 

することができますdetectorを使用してください同じようにあなたの希望NSRegularExpression

NSString *email = @"[email protected]";   
NSTextCheckingResult *match = [detector firstMatchInString:email options:0 range:NSMakeRange(0, email.length)]; 
NSLog(@"%@", match); 

(あなたがあなたの疑問に持っているように、有効であるかについての仮定に構築する正規表現を経由して、次のようによく知られているタイプに一致する多くの隠された落とし穴があります。サブドメインおよびマルチレベルTLDは確かに有効です)。可能な限り自分でやってはいけません。

0

[email protected]

NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

については、@記号の後にチェック複数のドットを除外する。 [email protected]: - 有効な

[email protected]:無効

は、あなたが使用していることを、@[A-Za-z0-9.-]

NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9-]+\\.[A-Za-z]{2,4}"; 
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
0

からの発現を、ドットを除外RFC 5322を満たしています。@の後の複数のドットは、ホスト名に対して完全に有効です。あなたが1つのドットに制限されている場合、あなたは多くのドメインをブロックしていました。

+ (BOOL) validateEmail: (NSString *) candidate; 
{ 
    // The Official Standard: RFC 5322 
    // http://www.regular-expressions.info/email.html 
    NSString *emailRegEx = 
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}" 
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\" 
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-" 
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5" 
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-" 
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21" 
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; 
    return [predicate evaluateWithObject:candidate]; 
}