简体   繁体   中英

How to enter the textfield.text in a special format

I have an app in which I want to enter first two charaters (Alphabets only)then space then next three characters numeric only,then space and next five characters numeric only again.

I know it has to be done in shouldchangecharacterinRange,textfield delegate method ,But what inside needs to be written is the real thing I am stuck on.I am a newbie in ios and not able to think about it right now. What is the correct way to achieve it.Kindly give suggestions.Thanks in advance!.

You have to implement the textfield delegate method [textfield shouldChangeCharacters replacementString][1]

[1]: https://developer.apple.com/reference/uikit/uitextfielddelegate/1619599-textfield ""

You need to write code to implement whatever format you want in textField:shouldChangeCharactersInRange:replacementString: method. this method called everytime user edit (type/delete/paste) in your text field.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
//range: range of character to be replaced. 
  // string : will give you replacement string--mostly one chracter if user is //typing 
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

this delegate will help you to check character that user enter depends you which you want to append or not in textfield /********edit************/

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

    if (range.length==1) {
        return YES;
    }



    if (textField.text.length<=12) {


        if ((range.location==0)||(range.location==1))
        {
            if([@"QWERTYUIOPLKJHGFDSAZXCVBNM" containsString:[string uppercaseString]]){

                return YES;

            }
        }
        else if (range.location==2)
        {
            if ([@"0123456789" containsString:string ]) {
                string = [NSString stringWithFormat:@" %@",string];
                textField.text =[NSString stringWithFormat:@"%@%@",textField.text,string];
                return NO;
            }
            else{
                return NO;
            }

        }
        else if(range.location<=5)
        {
            if ([@"0123456789" containsString:string ]) {
                string = [NSString stringWithFormat:@" %@",string];
                return YES;
            }
            else{
                return NO;
            }
        }
        else if (range.location>=6)
        {
            if (range.location==6) {
                if ([@"0123456789" containsString:string ]) {
                    string = [NSString stringWithFormat:@" %@",string];
                    textField.text =[NSString stringWithFormat:@"%@%@",textField.text,string];
                    return NO;
                }
            }
            else{
                if ([@"0123456789" containsString:string ]) {
                    string = [NSString stringWithFormat:@" %@",string];
                    return YES;
                }
                else
                {
                    return NO;
                }
            }

        }
    }
    return NO;
}
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

  if (textField== YourtextField)

{
    if(range.length > 0)
         NSString *str = @"Abcdefghijkl";
        textField.text = [NSString stringWithFormat:@"%@",[str substringToIndex:2]];
}

  if (textField==YourtextField) {
    YourtextField.text = @"1234567890"
    int length = (int) YourtextField.text;

    if(length == 10)
    {
        if(range.length == 0)
            return NO;
    }

    if(length == 3)
    {
        NSString *num = YourtextField.text;
        textField.text = [NSString stringWithFormat:@"(%@) ",num];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
    }
    else if(length == 6)
    {
        NSString *num = textField.text;
        textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];

        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
    }
}
return YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM