How to perform validation on textfield for phone number entered by user in iPhone?

↘锁芯ラ 提交于 2019-12-11 11:25:19

问题


I have an application where I have I a textfield where user enters his mobile number including his country code. The format of the mobile number to be entered is +91-9884715715. When the user enters his/her mobile number initially validation should be performed that the first value entered by user is '+' and then the number that is entered after + should not be less that 0.

But after this I am getting confused that how to get the number of numbers entered between + and -, because user enters the country code and the length of numbers entered between + and - must be dynamic not static.


回答1:


Try this ., might help you

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



回答2:


Try This:

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



回答3:


Goto XIB interface Builder and open xib document select ur phone number type textfield and go to textfield attribute, In the Text Input Traits, select Keyboard option from Default to Phone Pad.




回答4:


// limit the input to only the stuff in this character set, so no emoji or any other insane characters

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

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



回答5:


Refer @Bala's answer

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

Edit

Refer following link: TextField Validation With Regular Expression

Change the line

- (BOOL)validateInputWithString:(NSString *)aString
{
    NSString * const regularExpression = @"^([+]{1})([0-9]{2,6})([-]{1})([0-9]{10})$";
    NSError *error = NULL;

Add the code

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

Edit

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


来源:https://stackoverflow.com/questions/9919288/how-to-perform-validation-on-textfield-for-phone-number-entered-by-user-in-iphon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!