IOS开发-iOS中电话号码和邮箱判断
在iOS应用中我们经常会遇到一些个人信息的设置,需要判断用户输入的信息是否正确,电话号码和邮箱等是否符合要求,下面直接上代码:
1,判断字符串是否是电话号码:
- (BOOL)checkTelNumber:(NSString*)telNumber
{
NSString *pattern = @"^1+[3578]+\\d{9}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
BOOL isMatch = [pred evaluateWithObject:telNumber];
return isMatch;
}
2.判断字符串是否是邮箱地址:
-(BOOL)isValidateEmail:(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];
}
3.判断字符串是否都是有数字组成
- (BOOL)isAllNum:(NSString *)string{
unichar c;
for (int i=0; i<string.length; i++){
c=[string characterAtIndex:i];
if (!isdigit(c)) {
return NO;
}
}
return YES;
}
————————————————
版权声明:本文为CSDN博主「雪影无痕_1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29865447/article/details/52776256