// 正则表达式来判断当前的email是否合法
-(BOOL)validateEmail:(NSString *)email{
//取邮箱首位字符
NSString *str = [email substringToIndex:1];
NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];
//判断邮箱首字母是否为英文字母或数字
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];
}
// 正则表达式来判断当前的手机号是否合法
- (BOOL)validateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
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位字母或数字的组合
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;
}