1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rani
  • 3,333
  • 13
  • 48
  • 89

5 Answers5

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;
}
Bala
  • 2,895
  • 1
  • 19
  • 60
  • but how to check if the first value entered by the user should be +.if it is not + then it is not valid – Rani Mar 29 '12 at 05:28
  • i am having a problem i want to check if the phonenumber entered by user is less than 10 digits .If it is less then i am displaying an alertview .i have done the code 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]; [alert release]; } but the problem is when i enter '-' after my country code it shows alertview. – Rani Mar 29 '12 at 06:32
  • can you please post your code declare a those countryCode and phoneNumber string as .h file please do them in - (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField – Bala Mar 29 '12 at 06:37
  • only the phone number validation should be done in textfield End editing – Rani Mar 29 '12 at 06:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9449/discussion-between-rani-and-bala) – Rani Mar 29 '12 at 06:41
1

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);
Hector
  • 3,909
  • 2
  • 30
  • 46
0

// 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;
}
Akshatha S R
  • 1,237
  • 1
  • 15
  • 30
0

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");
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Hiren
  • 12,720
  • 7
  • 52
  • 72
  • if you need write only +,-,and number characters then i will updated my code – Hiren Mar 29 '12 at 05:55
  • i want to check if the first value entered by the user is '+'.If not then it is not valid.Between + and - there will be minimum two digits for country code and after - mobile number is which will be of ten digits – Rani Mar 29 '12 at 06:37
  • could u help me please actually this is working but i am having little bit problem i.e my country code which is second part of regular expression is dynamic i.e user can enter code which length will be >= {2} and <={6} but not less {2} and mobile number should be strictly 10 digits long – Rani Mar 29 '12 at 09:31
0

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.

Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41