如何在iphone上对用户input的电话号码进行textfieldvalidation
我有一个应用程序,我有一个文本框,用户input他的手机号码,包括他的国家代码。input手机号码的格式是+ 91-9884715715。当用户input他/她的手机号码时,首先应该执行validation用户input的第一个值是'+',然后在+之后input的数字不应该小于0.但是在这之后,我很困惑如何获得在+和 – 之间input的数字的号码,因为用户input国家代码和+和 – 之间input的数字的长度必须是dynamic的而不是静态的。请帮我解决这个问题。谢谢
试试这个,可能对你有帮助
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (textField == self.yourphoneNumberfield) { NSArray *sep = [newString componentsSeparatedByString:@"-"]; if([sep count] >= 2) { countryCode = [NSString stringWithFormat:@"%@",[sep objectAtIndex:0]]; if ([[countryCode substringToIndex:1] isEqualToString:@"+"]) { phoneNumber = [NSString stringWithFormat:@"%@",[sep objectAtIndex:1]]; return ([countryCode length]+[phoneNumber length]); } } } return YES; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ NSLog(@"Phone Number : %@",phoneNumber); if (textField == self.yourphoneNumberfield) { if ([phoneNumber length]<10) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Please Enter a Valid Mobile number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } } return YES; }
尝试这个:
NSString *code=@"+91-99999999"; NSRange rr2 = [code rangeOfString:@"+"]; NSRange rr3 = [code rangeOfString:@"-"]; int lengt = rr3.location - rr2.location - rr2.length; int location = rr2.location + rr2.length; NSRange aa; aa.location = location; aa.length = lengt; code = [code substringWithRange:aa]; NSLog(@"%@",code);
转到XIB界面生成器并打开xib文档select您的电话号码types文本字段,并转到文本字段属性,在文本input特征,从默认到电话簿select键盘选项。
//将input限制在这个字符集中的东西,所以没有表情符号或任何其他疯狂的字符
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"]; if ([string rangeOfCharacterFromSet:set].location == NSNotFound) { return NO; }
请参阅@巴拉的答案
NSString *call = @"+91-9884715715"; // Search for the "+a" starting at the end of string NSRange range = [call rangeOfString:@"+" options:NSBackwardsSearch]; // What did we find if (range.length > 0) NSLog(@"Range is: %@", NSStringFromRange(range));
编辑
参考下面的链接使用正则expression式的TextFieldvalidation
改行
- (BOOL)validateInputWithString:(NSString *)aString { NSString * const regularExpression = @"^([+]{1})([0-9]{2,6})([-]{1})([0-9]{10})$"; NSError *error = NULL;
添加代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { char *x = (char*)[string UTF8String]; //NSLog(@"char index is %i",x[0]); if([string isEqualToString:@"-"] || [string isEqualToString:@"+"] || [string isEqualToString:@"0"] || [string isEqualToString:@"1"] || [string isEqualToString:@"2"] || [string isEqualToString:@"3"] || [string isEqualToString:@"4"] || [string isEqualToString:@"5"] || [string isEqualToString:@"6"] || [string isEqualToString:@"7"] || [string isEqualToString:@"8"] || [string isEqualToString:@"9"] || x[0]==0 ) { NSUInteger newLength = [textField.text length] + [string length] - range.length; return (newLength > 18) ? NO : YES; } else { return NO; } }
编辑:testing与演示
//// Button Press Event -(IBAction)Check:(id)sender{ BOOL check = [self validateInputWithString:TextField.text]; if(check == YES){ NSLog(@"Hii"); NSString *string= [NSString stringWithFormat:@"%@", TextField.text]; NSArray *first = [string componentsSeparatedByString:@"-"]; NSString *second = [first objectAtIndex:1]; NSString *third = [first objectAtIndex:0]; if([second length] < 11){ NSLog(@"bang"); } else{ NSLog(@"Fault"); } if([third length] > 3 || [third length] < 7){ NSLog(@"Bang"); } else{ NSLog(@"fault"); } } else{ NSLog(@"FAULT"); } }