Rani
Rani

Reputation: 3453

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

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.

Upvotes: 1

Views: 13868

Answers (5)

Hiren
Hiren

Reputation: 12780

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

Upvotes: 0

Akshatha S R
Akshatha S R

Reputation: 1345

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

Upvotes: 0

Hector
Hector

Reputation: 3907

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

Upvotes: 1

Bala
Bala

Reputation: 2895

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

Upvotes: 1

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

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.

Upvotes: 0

Related Questions