2

I implement this code but backspace not working until i press delete button in UITextfield I want to do with Backspace delete.

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

    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:_demo_str];
    for (int i = 0; i < [string length]; i++) {
         unichar c = [string characterAtIndex:i];
         if ([myCharSet characterIsMember:c]) {
            return YES;
      }
    }
    return NO;
  }

2 Answers2

0

Try this code. this working for me

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

  NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:_demo_str];
  for (int i = 0; i < [string length]; i++) {
     unichar c = [string characterAtIndex:i];
     if ([myCharSet characterIsMember:c]) {
        return YES;
     }
     else
     {
         return NO;
     }
  }
   return YES;
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
-1

use this code:

NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString: _demo_str];
if(range.length > 0){
    
    if( !string || [string isEqualToString:@""] || string.length == 0){
          return YES;
    }
    else{
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if ([myCharSet characterIsMember:c]) {
                return YES;
            }
        }
    }

     return NO;
}
else{
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            return YES;
        }
    }
      return NO;
}
shim
  • 9,289
  • 12
  • 69
  • 108
SGDev
  • 2,256
  • 2
  • 13
  • 29
  • `range.length` will be `1` in conditions other than tapping the backspace key. And the length can be other than `1` when the backspace key is tapped. This answer won't work. – rmaddy Dec 06 '14 at 07:04
  • can you explain me one condition where it is not work. – SGDev Dec 06 '14 at 07:07
  • I can give you two. 1) Highlight one character in the text field and tap a letter. `range.length` is `1` but the backspace wasn't tapped. 2) Highlight two or more characters in the text field and tap backspace. `range.length` won't be `1` but the backspace was tapped. – rmaddy Dec 06 '14 at 07:09
  • In other words, `range.length` has nothing to do with whether the backspace was tapped or not. – rmaddy Dec 06 '14 at 07:11
  • Read my last two comments. Your change is still wrong. – rmaddy Dec 06 '14 at 07:22
  • Right @rmaddy range.length not working see my answer it's working for me – Mayank Patel Dec 06 '14 at 07:47
  • i am agree with you "range.length has nothing to do with whether the backspace was tapped or not".now see my latest code it will work in all cases – SGDev Dec 06 '14 at 07:50
  • 1
    Your latest update is still wrong. There is no need to check `range.length` at all. And your overly long `if` statement checking `string` should simply be `if (!string.length)`. – rmaddy Dec 06 '14 at 17:48
  • @SumitGarg it's not working when value between 1000 to 5000 try it ? –  Dec 10 '14 at 04:50
  • @rmaddy not working when range between 1000 to 5000 i tried it .i accept answer it works when we enter value between 5 to 7 not those range 1000 to 5000 –  Dec 10 '14 at 04:54
  • @rmaddy this answer's not wrong but if value between 5000 to 7000 then it now working because it find the value in string not the range –  Dec 10 '14 at 04:55
  • @iOS You need to ask a new question with the relevant code causing your problem. – rmaddy Dec 10 '14 at 04:55