如何在文本字段中对用户在iPhone中输入的电话号码进行验证?

时间:2012-03-29 04:58:10

标签: iphone objective-c ios ipad uitextfield

我有一个应用程序,我有一个文本字段,用户输入他的手机号码,包括他的国家代码。要输入的手机号码的格式为+ 91-9884715715。当用户输入他/她的手机号码时,最初应验证用户输入的第一个值是“+”,然后在+之后输入的数字不应小于0。

但在此之后我感到困惑的是如何获得在+和 - 之间输入的数字的数量,因为用户输入国家代码并且在+和 - 之间输入的数字长度必须是动态的而不是静态的。

5 个答案:

答案 0 :(得分:1)

试试这个。,可能会帮到你

- (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;
}

答案 1 :(得分:1)

试试这个:

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);

答案 2 :(得分:0)

参考@ Bala的回答

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));

修改

请参阅以下链接:TextField Validation With Regular Expression

更改行

- (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;
    }
}

修改

使用demo进行测试:

  //// 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");
    }
}

答案 3 :(得分:0)

转到XIB界面Builder并打开xib文档选择你的电话号码类型textfield并转到textfield属性,在Text Input Traits中,选择Keyboard默认为Phone Pad。

答案 4 :(得分:0)

//仅将输入限制为此字符集中的内容,因此没有表情符号或任何其他疯狂字符

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];

if ([string rangeOfCharacterFromSet:set].location == NSNotFound) {
  return NO;
}