iOS 常用的正则
2018-11-01 本文已影响0人
money_ac9e
常用的正则
// 邮箱验证
+ (BOOL)isEmail:(NSString *)email;
// 用户名验证
+ (BOOL)isUserName:(NSString *)username;
// 昵称验证
+ (BOOL)isNickname:(NSString *)nickname;
// 用户简介
+ (BOOL)isBrief:(NSString *)Brief;
// 电话号验证
+ (BOOL)isTelephone:(NSString *)telePhone;
// 手机号验证
+ (BOOL)isTeleNumber:(NSString *)telenumber;
// QQ验证
+ (BOOL)isQQMobile:(NSString *)mobile;
// 邮编验证
+ (BOOL)isZipCode:(NSString *)zipCode;
// 判断是否为正整数或小数
+ (BOOL)isPositiveIntegerOrDecimal:(NSString*)value;
// 判断是否为正整数
+ (BOOL)isPositiveInteger:(NSString*)value;
/**
判断至多两位小数
@param money
@return YES/NO
*/
+ (BOOL)isValidateMoney:(NSString *)money;
+ (BOOL)isEmail:(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)isUserName:(NSString *)username
{
NSString *phoneRegex = @"[0-9a-zA-Z_]{6,11}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:username];
}
+ (BOOL)isNickname:(NSString *)nickname
{
if (nickname.length < 1 || nickname.length > 11) {
return NO;
}else{
return YES;
}
}
+ (BOOL)isBrief:(NSString *)Brief
{
if (Brief.length == 0 || Brief.length > 60) {
return NO;
}else{
return YES;
}
}
+ (BOOL)isTeleNumber:(NSString *)telenumber
{
NSString *regex = @"^1\\d{10}$";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [mobileTest evaluateWithObject:telenumber];
}
// 电话号验证
+ (BOOL)isTelephone:(NSString *)telePhone
{
NSString *phoneRegex = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [regextestct evaluateWithObject:telePhone];
}
+ (BOOL)isQQMobile:(NSString *)mobile
{
NSString *phoneRegex = @"^[1-9]*[1-9][0-9]*$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
+ (BOOL)isZipCode:(NSString *)zipCode
{
NSString *phoneRegex = @"^[1-9]\\d{5}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:zipCode];
}
// 判断是否为正整数或小数
+ (BOOL)isPositiveIntegerOrDecimal:(NSString*)value
{
NSString * regex = @"^(?:[1-9][0-9]*(?:\\.[0-9]+)?|0(?:\\.[0-9]+)?)$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:value];
return isMatch;
}
// 判断是否为正整数
+ (BOOL)isPositiveInteger:(NSString*)value
{
NSString * regex = @"^\\d+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:value];
return isMatch;
}
/**
判断至多两位小数
@param money
@return YES/NO
*/
+ (BOOL)isValidateMoney:(NSString *)money
{
NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:money];
}