2

I have a UITextfield in which I want user to enter comma separated text

example : elephant,fox, etc, Also I want to restrict the user to enter at maximum 7 comma separated words.

I have written this code in the delegate method of UITextfield

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  NSArray *numberOfWords = [self.tagsTextField.text componentsSeparatedByString:@","];
    if([numberOfWords count] > 6)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

I am unable to achieve the desired behaviour please help!

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • maybe you want to use a simple regex pattern for this? This could help: [link] (https://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet) – Ahmed Hasn. Mar 16 '17 at 10:34
  • 1
    You try to get word count in unchanged text. You need to apply changes then try to get words count. **[self.tagsTextField.text stringByReplacingCharactersInRange:range withString:string]**. But I think it's bad user experience. I as user don't like when I can't apply some change and application don't tell me what wrong. Allow enter more worlds but show error. – Sergey Mar 16 '17 at 10:37
  • what is issue in above code? – Birendra Mar 16 '17 at 10:43

3 Answers3

0

try this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:  (NSRange)range replacementString:(NSString *)string
  {
 NSArray *numberOfWords = [self.tagsTextField.text componentsSeparatedByString:@","];
if([numberOfWords count] > 6 && [string isEqualToString:@","])
{
    return NO;
}
else
{
    return YES;
}
}

You can block second spaces and something like that.

Onik IV
  • 5,007
  • 2
  • 18
  • 23
0

In your code, it'll restrict user to enter the seventh word and also it'll not allow user to erase after entering 6th word as well.

Try this code,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSArray *numberOfWords = [self.textField.text componentsSeparatedByString:@","];
    if([numberOfWords count] > 7)
    {
        if ([string isEqualToString:@""]) {
        return YES;
        } else {
        return NO;
        }

    }
    else
    {
        return YES;
    }
}

This will allow user to enter upto 7 words by comma separator and you can also delete words after entering 7 words.

iPeter
  • 1,330
  • 10
  • 26
0

please follow the bellow code: It will take 7 comma after 7 comma it wouldn't take any but you can write or delete the characters in the textfield.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSArray *numberOfWords = [myTextField.text componentsSeparatedByString:@","];


    NSLog(@"%d",(int)numberOfWords.count);

    NSLog(@"%@",numberOfWords);

    if([numberOfWords count] > 7 && [string isEqualToString:@","])
    {
        return NO;
    }
    else
    {
        return YES;
    }

}

enter image description here

Hope this help you..

UPDATE:

This code won't work in case of pasting copied text in the textfield. To disable copy/pasting for the textField use the following code : Already Mentioned here

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:))
        return NO;
    return [super canPerformAction:action withSender:sender];
}
Community
  • 1
  • 1
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • Lets try to select **3,4,5**, copy it then got to the end line and paste it. – Sergey Mar 16 '17 at 11:32
  • @Sergey Do you think i copied it :/ ? well after getting this question, i started trying by doing my self and coincidently matched with another answer. when I posted it, even i didn't see any post till then, after refreshing the page i saw two more answer has been given already. :/ – Abhishek Mitra Mar 16 '17 at 11:40
  • No, I don't think that you copy answer. I try to explain you that your realisation have a bug, however other realisation have this bug too. – Sergey Mar 16 '17 at 11:44
  • @SergeyOh, pardon me, I didn't think this in your way, I have tried and found the same what you have mentioned, then my and other's answer are wrong too. if we disable the copy paste thing for that textfield then it will be alright. :) . – Abhishek Mitra Mar 16 '17 at 11:52
  • You can change your answer to solve this problem, you can see my comment in question section, but I don't like this realisation. – Sergey Mar 16 '17 at 11:54