textfield inputs range in interface builder - iphone

I have multiple textfields in my nib file.
I want to decide the input range in my one textfield to 6-16 digits and I don't want to change any other textfield input. For that I made a method called tflimit as below.
-(IBAction)tflimit:(id)sender
{
if([textfields1.text length]>=15 )
{
[textfields1 resignFirstResponder];
}
}
With this method I can input only 16 digits input.
How can I decide the range(6-16) of an input in the textfield without changing other codes.

You can filter user input in textField:shouldChangeCharactersInRange:replacementString: method in text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == textfields1){// Apply logic only to required field
NSString* newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return [newString length] < 16 && [newString length] > 5;
}
return YES;
}
Note that to work correctly this method require textfield to be pre-populated with text at least 5 characters long.

Related

Calling a method only after shouldChangeCharactersInRange returns yes

I have a UITextField with a table below it showing a list of items to select from (similar to drop down list). Now for example when i type 2 in textfield (textfield has Year values), the table would show all strings with 2 as substring. So when i type in 2000, it would only show matching string 2000 in the table.
Now when i finish typing 2000 in the textfield i want to call a method. Everything works fine but i want to call this method only when i finish typing all 4 digits but here the method is called when i try to enter 4th digit.
How can i perform this where i type in 2000 and it will call the method after shouldChangeCharactersInRange return Yes after entering 3rd zero.
Here's my code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
//if _filteredarray count==1 and substring and _filteredarray object at index 0 matches then call a method here
return YES;
}
Try this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
if(substring.length == 4)
{
[textField resignFirstresponder];
[self performSelector:#selector(functiontocall)withObject:nil afterDelay:0.8];
}
return YES;
}
the above code may give u an idea
if u enter fourth letter of 2000, then the keyboard will disappear(if u want u can add it, which will avoid further entering values to textfield), then u can see the third zero for 0.8 seconds and the function u need to call will be called.

How to check that any of the textfields inputed values is not number and is negative then show message

I have many textField Inputs for the calculations I want that if any of the inputs is not a number and negative number then it show error message on button click and not to move to the other screen
There's at least a couple ways you can handle this, James.
#1) specify numeric keyboards for those specific numeric-only text fields in the storyboard / xib file
#2) set a delegate for the text field and when the user is done editing (e.g. textFieldDidEndEditing:), look at the contents of the text field and if you see anything that isn't a number, throw up a UIAlert.
One way to do this would be:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSRange rangeOfInvalidCharacter = [textField.text rangeOfCharacterFromSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
if(rangeOfInvalidCharacter.location != NSNotFound)
{
// throw up a UIAlert here
// and, if you want, erase the bogus text via:
textField.text = #"";
return;
}
// and if we get here, that means the text field contents are only digits.
}
Try this code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
{
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
return NO; // For don't add this character
}
return YES; // For input a true character
}

shouldChangeCharactersInRange taking only one digit at time

i am trying to get total value after multiplying price and quantity in to text field. I not getting value when quantity is 10 or having any two or three digits.This method takes only one character at time.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
if(textField == quantityText)
{
NSCharacterSet *charactersToRemove =[[ NSCharacterSet alphanumericCharacterSet ]
invertedSet];
NSRange inRange=[string rangeOfCharacterFromSet:charactersToRemove];
if(inRange.location != NSNotFound)
{
quantityText.text =[ quantityText.text
stringByTrimmingCharactersInSet:charactersToRemove ];
return NO;
}
if ([textField text] )
{
float quantity = [string floatValue];
float price = [[priceLabel text] floatValue];
float h = quantity * price;
amountText.text=[NSString stringWithFormat:#"%f",h];
}
else
{
return NO;
}
}
return YES;
}
You're only using the replacementString value for your calculation, which is the last character that was typed, not the whole the whole string.
So if I type '1' then function uses 1 as the value, then if I type '0' to make 10, your function only uses the '0' as the value.
You need to get the whole text of the quantityText textfield and use that. You could get that by taking textField.text and then replacing the specified range with the replacementString.
To be honest though it's a lot easier just to register for the UITextFieldTextDidChangeNotification instead of using the textfield:shouldChangeCharactersInRange:replacementString: method.
See this answer for details.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
this delegate method is called whenever user types a new character in to the textfield and the string object will contain only the last typed character. so instead of using (NSString *)string use textField.text

How to verify UITextField text?

I need to make sure that the user is only entering numbers into my textfield. I have the keyboard set to numbers, but if the user is using an external keyboard they might enter a letter. How can I detect if any characters in my textfield.text are characters instead of numbers?
Thanks!
You can choose what characters can input into the textField
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
/* this ensures that ONLY numbers can be entered, no matter what kind of keyboard is used */
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
return NO;
}
}
/* this allows you to choose how many characters can be used in the textField */
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 7) ? NO : YES;
}
Whenever the user enters a key this textfield delegate will be called.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Inside this check whether the text contains characters. if it is do your action. like promptimg a alert or something.
Implement textField:shouldChangeCharactersInRange:replacementString: in the text field's delegate and return NO if the passed string contains invalid characters.

delete last character UITextField

I have an UITextField and I would like that for every tap on a character, the first character is deleted. So that I just have one character in my textField every time. Moreover I would like it to display every tap in the console log.
How can I do this?
You need to implement shouldChangeCharactersInRange method in your text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
textField.text = #"";
return YES;
}
You may need to check for range and string values to cover all possible cases (like copy/paste actions). This code just sets the text field's value to the last typed character.
UITextField inherits from UIControl, so you can use the target-action mechanism that is part of the UIControl class:
[textField addTarget:self action:#selector(updateTextField) forControlEvents:UIControlEventValueChanged];
In the action method, you can replace the UITextField's text with only the last character and log that character in the console. Note that since changing the UITextField's text will again result in the "updateTextField" message being sent a second time to the target, you will need some kind of mechanism for determining whether to update or not:
- (void)updateTextField {
if(updateTextField == YES) {
updateTextField = NO;
NSString *lastChar = [textField.text substringFromIndex:[textField.text length]];
[textField setText:lastChar];
NSLog(#"%#", lastChar);
} else {
updateTextField = YES;
}
}
Or something like that anyway...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length > 8) {
return NO;
}
return YES;
}