-1

I want to add prefix of UITextfield text. The UITextfield text length less than 7. How many characters are less than 7, that all replace with zeros.

If text is "1234", add prefix like "0001234". If text is "12345", add prefix like "0012345". If text is "123", add prefix like "0000123".

can any one suggest me, how to implement.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • 1
    Sounds nasty to use; If I enter `1` and you set it to `0000001` then there is no room to enter `12`, etc. How would it work? Why is it important that the leading zeroes exist in the text field? – Droppy Jul 08 '15 at 12:28
  • 1
    The only use I know of for doing that is a calculator app. Even then, leading zeroes aren't really necessary. I don't like this question because it just asks people for code instead of showing that you tried to make your own solution. That's not how Stack Overflow works. That's not how any of this works. – DDPWNAGE Jul 08 '15 at 12:36
  • @Droppy while you've correctly identified a problem, it could easily be resolved in `shouldChangeCharacters` (and various other means) (and we'd have to implement this method anyway to prevent the user from entering more than 7 characters, etc). – nhgrif Jul 08 '15 at 13:08
  • @nhgrif Well you say "easily" and yet nobody has solved it yet with their answers... – Droppy Jul 08 '15 at 13:13
  • @Droppy My answer should solve that problem now. – nhgrif Jul 08 '15 at 13:36

4 Answers4

1

It sounds like what we actually want is a numbers-only string that is always 7-characters long, with the left-most characters filled in with padded zeros for anything the user has not entered, correct?

So, we need a handful of methods to make this as easy as possible.

First, this one doesn't make sense right now, but we want a method to remove the zeros we padded at the front (it'll make sense later).

So, borrowing from this Stack Overflow answer...

- (NSString *)stringByRemovingPaddedZeros:(NSString *)string {
    NSRange range = [string rangeOfString:@"^0*" options:NSRegularExpressionSearch];
    return [string stringByReplacingCharactersInRange:range withString:@""];
}

And we'll borrow from Ilesh's answer for adding the padded zeros:

- (NSString *)stringByAddingPaddedZeros:(NSString *)string padLength:(NSInteger)length {
    NSString *padding = [@"" stringByPaddingToLength:(length - string.length) withString:@"0" startingAtIndex:0];
    return [NSString stringWithFormat:@"%@%@", padding, string];
}

So now we can go back and forth between padded and unpadded strings, right?

So now, one last step, implementing shouldChangeCharactersInRange:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    newString = [self stringByRemovingPaddedZeros:newString];
    newString = [self stringByAddedPaddedZeros:newString padLength:7];
    textField.text = [newString subStringToIndex:7];

    return NO;
}

We always return NO here, as we're setting the textField.text property manually. Now when there are 7 characters (and no leading zeros), the user can type no more. If there are 7 characters and the user hits backspace, they should all shift right one and a zero added to the front. If there are leading zeros at the front, typing characters should shift everything left and drop a leading zero, and add a new character to the front.

As an additional note, this code does not take care of verifying that the user is only entering digits. Some extra logic would be required for that. I'd simply recommend checking that the replacementString (string) is only digits before you get into any of the other code in shouldChangeCharactersInRange here.

Community
  • 1
  • 1
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Very nice. You are a lazy programmer's dream ;-) – Droppy Jul 08 '15 at 13:37
  • @Droppy if this weren't an extraordinarily common design pattern for masking text fields, I'd have different thoughts about such a complete answer. – nhgrif Jul 08 '15 at 13:48
0

Here printing textfield text on button click. Check the code inside the method.

- (IBAction)logTextFieldText:(id)sender
{

   NSMutableString *str=[[NSMutableString alloc]init];

   if (_txtf.text.length<7)
   {

      for (int i=0;i<7-_txtf.text.length; i++)
      {
         [str appendString:@"0"];
      }
      [str appendString:_txtf.text];
   }

   NSLog(@"final text is: %@",str);

}
user4261201
  • 2,324
  • 19
  • 26
0

Implement the UITextFieldDelegate method textFieldDidEndEditing: to pad the 0's in.

- (void)textFieldDidEndEditing:(nonnull UITextField *)textField
{
    if (textField.text.length < 7) {
        // Create a string of 0's to pad with
        NSString *padding = [@"" stringByPaddingToLength:(7 - textField.text.length) withString:@"0" startingAtIndex:0];
        NSMutableString *change = [textField.text mutableCopy];
        // Insert the 0's string
        [change insertString:padding atIndex:0];
        textField.text = change;
    }
}
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
0

If you want to fix the length of UITextField text than use this UITextField delegate method.

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (self.txtGet.text.length>=7) {
        return NO;
    }
    return YES;
}

and the completion editing (or done button ) you add this line in before using the UITextField value.

 NSString *padding = [@"" stringByPaddingToLength:(7 - self.txtGet.text.length) withString:@"0" startingAtIndex:0];
    NSMutableString *change = [self.txtGet.text mutableCopy];
    // Insert the 0's string
    [change insertString:padding atIndex:0];
    self.txtGet.text = change;

I think its helpful to you. Thank you.

Ilesh P
  • 3,940
  • 1
  • 24
  • 49