IOS 手机号、电话号码、邮箱、网址、邮编的验证

//邮箱

+ (BOOL) validateEmail:(NSString *)email

{

NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}

//手机号码验证

+ (BOOL) validateMobile:(NSString *)mobile

{

//手机号以13, 15,18开头,八个 \d 数字字符

NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobile];

}

//座机号码验证

+ (BOOL) validateTelphone:(NSString *)telphone

{

NSString *phoneRegex = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:telphone];

}

//URL验证

+ (BOOL) validateUrl:(NSString *)url

{

NSString *regex = @"http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

return [pred evaluateWithObject:url];

}

//邮编验证

+ (BOOL) validateZipCode:(NSString *)zipCode

{

NSString *regex = @"[0-9]\\d{5}(?!\\d)";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

return [pred evaluateWithObject:zipCode];

}

你可能感兴趣的:(IOS 手机号、电话号码、邮箱、网址、邮编的验证)