-4

I have three Textfield.

1.textField1 = 15 charecter

2.textField2 = 50 charecter

3.textField3 = 50 charecter

Code snippet

if (textField1 .text.length <15 && textField2 .text.length <50 && textField3 .text.length <50) {

    return YES;        

    }else{

        return NO;
    }

How to set the limit of three UITextfield.

Thanks in advance

Nothing
  • 27
  • 1
  • 4

3 Answers3

0

This delegate permit to limit to nbChar a Uitexfield :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField.text length] >=nbChar) {
        textField.text = [textField.text substringToIndex:nbChar];
        return NO;
    }
    return YES;
}
zbMax
  • 2,756
  • 3
  • 21
  • 42
0

try like this,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSUInteger length = [textField.text length] + [string length] - range.length;

    if([textField  isEqual:textField1])
    {
        if(length<15)
            return YES;
        else
            return NO;
        NSLog(@"1");
    }
    else  if([textField  isEqual:textField2] | [textField  isEqual:textField3])
    {
        if(length<50)
            return YES;
        else
            return NO;
        NSLog(@"2 or 3");

    }


}
Balu
  • 8,470
  • 2
  • 24
  • 41
0

The Problem with some of the answer given above is, For example I have a text field and I have to set a limit of 15 characters input, then it stops after entering 15th Character. but they Don't allow to delete. That is the delete button also don't work. As I was facing the same problem. Came out with the solution , Given Below. Works Perfect for Me

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 if(textField.tag==6)
 {
    if ([textField.text length]<=30)
    {
        return YES;   
    }
    else if([@"" isEqualToString:string])
    {
        textField.text=[textField.text substringToIndex:30 ];
    }

    return NO;
 }
 else
 {
    return YES;
 }
}

I am having a text field, whose tag I have set "6" and I have restricted the max char limit = 30 ; works fine in every case. In the same way you can set tag for other textfields and define limit over them in the same way.

Jasmeet
  • 1,522
  • 2
  • 22
  • 41