Kazik
Kazik

Reputation: 21

UITextField with UIKeyboardType.NumberPad and nonnumeric text

I'm using TextField to get from user number in specific form xxx-xxx-xx-xx.

I need to show user NumberPad keyboard and add separators "-", after 3,6 and 8th digit during editing.

The problem is that when i specify

TextField.keyboardType = UIKeyboardType.NumberPad

and add separator in my textFieldDidChange method, TextField stops responding to adding next character or remove.

Changing to UIKeyboardType.Default works perfectly, but keyboard is not digit only.

Upvotes: 0

Views: 321

Answers (3)

Kazik
Kazik

Reputation: 21

In shouldChangeCharactersInRange method works fine. Thx.

Upvotes: 0

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

Use this below UITextField delegate method below,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{   
    if (textField == numberTextField)
    {
        if (range.location == 12) {
            return NO;
        }

        // Reject appending non-digit characters
        if (range.length == 0 &&
            ![[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[string characterAtIndex:0]]) {
            return NO;
        }

        // Auto-add hyphen before appending 4rd or 7th digit or 10th digit
        if (range.length == 0 &&
            (range.location == 3 || range.location == 7 || range.location == 10)) {
            textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string];
            return NO;
        }

        // Delete hyphen when deleting its trailing digit
        if (range.length == 1 &&
            (range.location == 4 || range.location == 8))  {
            range.location--;
            range.length = 2;
            textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }

        return YES;
    }
    return YES;
}

hope its helpful

Upvotes: 0

Jay Mehta
Jay Mehta

Reputation: 1451

Hi You can use below code.

txtField.keyboardType = UIKeyboardTypeNumberPad;

- (BOOL)textFieldPhoneDigit:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

@try
{
    NSString *filter = @"(###) - (###) - (####)"; //Change Fileter As Per requirement. 

    if(!filter) return YES; // No filter provided, allow anything

    NSString *changedString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(range.length == 1 && // Only do for single deletes
       string.length < range.length &&
       [[textField.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]].location == NSNotFound)
    {
        // Something was deleted.  Delete past the previous number
        NSInteger location = changedString.length-1;
        if(location > 0)
        {
            for(; location > 0; location--)
            {
                if(isdigit([changedString characterAtIndex:location]))
                {
                    break;
                }
            }
            changedString = [changedString substringToIndex:location];
        }
    }

    textField.text = filteredPhoneStringFromStringWithFilter(changedString, filter);

    return NO;

}
@catch (NSException *exception) {
    NSLog(@"Exception shouldChange %@",[exception description]);
}

Upvotes: 1

Related Questions