要看你手机号怎么定义的,我认为如果是NSNumber类型,只需要看它长度就可以了,如果是NSString,就要转成char比对是不是数字的asc码了。当然个人认为这些单独写一个方法比较好。
如果有别的比对要求,比如开头151,152,,,,什么的,建议把这些存一个数组,然后切割NSString字符串,重写isEqualToString,遍历比较。
//7.合法手机号 要求:手机号以1开头总共有11位 并且手机号中不能有除了数字以外的其他字符
-(BOOL)isPhoneNumberOfString:(NSString *)phoneString{
NSMutableString *str3=[[NSMutableString alloc]init];
int count=0,sum;
NSString *str1=[phoneString substringToIndex:1];
if ([str1 isEqualToString:@"1"]==YES) {
++sum;
[str3 appendString:@"第一个号码为:1"];
// BOOL ret=[[str3 substringWithRange:NSMakeRange(0, 1)]isEqualToString:@"1"];
}
//
for (NSUInteger i=0; i<[phoneString length]; i++) {
unichar c=[phoneString characterAtIndex:i];
int set=(int)c-48;
if(c>='0'&&c<='9'){
++count;
NSLog(@"%d",count);
}
}
NSLog(@"(7)手机号码全为数字的位数:%d %@",count,str3);
NSLog(@"如何号码符合返回1:如何号码不符合返回0:%d",(count==11&&sum==1));
return (count==11&&sum==1);
}