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

2018-05-12  本文已影响12人  hjltony

邮箱

//邮箱+ (BOOL) validateEmail:(NSString*)email{NSString*emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"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 = [NSPredicatepredicateWithFormat:@"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 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];return[phoneTest evaluateWithObject:telphone];}

URL验证

//URL验证+ (BOOL) validateUrl:(NSString*)url{NSString*regex =@"http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?";NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];return[pred evaluateWithObject:url];}

邮编验证

//邮编验证+ (BOOL) validateZipCode:(NSString*)zipCode{NSString*regex =@"[0-9]\\d{5}(?!\\d)";NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];return[pred evaluateWithObject:zipCode];}

作者:frolaqi

链接:https://www.jianshu.com/p/2325e4c6fad3

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇下一篇

猜你喜欢

热点阅读