Skip to content

Instantly share code, notes, and snippets.

@Shosta
Created October 4, 2013 07:53
Show Gist options
  • Save Shosta/6822435 to your computer and use it in GitHub Desktop.
Save Shosta/6822435 to your computer and use it in GitHub Desktop.
NSString category to test if a string is a valid e-mail address.
//
// NSString+EmailValidating.h
//
//
// Created by Rems on 11/12/12.
// Copyright (c) 2012 . All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (EmailValidating)
#pragma mark - Valid email
//! Valid if a NSString ivar is a valid email address.
//! @param [in] string : The NSString to check if it is a valid email address.
- (BOOL)isValidEmail;
@end
//
// NSString+EmailValidating.m
//
//
// Created by Rems on 11/12/12.
// Copyright (c) 2012 . All rights reserved.
//
#import "NSString+EmailValidating.h"
@implementation NSString (EmailValidating)
#pragma mark - Valid email
/**
@brief Valid if a NSString ivar is a valid email address.
@author : Rémi Lavedrine
@date : 11/12/2012
@remarks : <#(optional)#>
*/
- (BOOL)isValidEmail
{
BOOL stricterFilter = YES; // 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-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment