// 正則表達式來判斷當(dāng)前的email是否合法
-(BOOL)validateEmail:(NSString *)email{
//取郵箱首位字符
NSString *str = [email substringToIndex:1];
NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];
//判斷郵箱首字母是否為英文字母或數(shù)字
NSRange userNameRange = [str rangeOfCharacterFromSet:nameCharacters];
if (userNameRange.location != NSNotFound) {
return NO;
}
NSString *emailRegex = @"[A-Z0-9a-z_.]+@[A-Za-z0-9]+\\.[A-Za-z]{1,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
// 正則表達式來判斷當(dāng)前的手機號是否合法
- (BOOL)validateMobile:(NSString *)mobile
{
//手機號以13降允, 15刑桑,18開頭藕溅,八個 \d 數(shù)字字符
NSString *phoneRegex = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//判斷是否含有特殊字符
-(BOOL)validateSpecialCharacters:(NSString *)characters
{
NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];
NSRange userNameRange = [characters rangeOfCharacterFromSet:nameCharacters];
if (userNameRange.location != NSNotFound) {
return NO;
}
return YES;
}
//判斷密碼是否合法
- (BOOL) validateUserPasswd : (NSString *) str
{
if (str) {
//密碼改為8到16位字母或數(shù)字的組合
NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
initWithPattern:@"^[a-zA-Z0-9]{8,16}$"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:str
options:NSMatchingReportProgress
range:NSMakeRange(0, str.length)];
if(numberofMatch > 0)
{return YES;}}
return NO;}
//判斷是否含有漢字
-(BOOL)validateCharacters:(NSString *)characters{
NSString *text = characters;unsigned long length = [text length];for (int i=0; i<length; i++){
NSRange range = NSMakeRange(i, 1);
NSString *subString = [text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) == 3)
{
return NO;
}
return YES;
}