UITextField textFieldShouldChange does not give full string - iphone

My code is
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(#"%#",petTypeTextFeild.text);
return YES;
}
when i type 'a' nothing is printed
when i typenext letter 's' 'a' is printed
I want full text.
Is there any other methode like textFinishChange?
Thank You.

Use the following:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"%#",newString);
return YES;
}

In that method you can't get full text..
If you return Yes then only that particular letter will be allowed and if return No it will not allow to change the text.
And use this method to get complete text.
- (void)textFieldDidEndEditing:(UITextField *)textField

This is a delegate which is called before textField actually changes its value,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Thats Why you are getting old value, So you should use this line to get the exact value
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"%#",str);
return YES;
}

Related

Filter out characters in one UITextField

Ok, I am trying to edit this one text field that is txtCountry to not accept certain letters, but I can't, because the txt country is not setup right. Can someone please fix this up for me. I probably making some kind of little detail mistake.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([textField isEqual: txtCountry]) {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEAGELNUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:#""];
BOOL basicTest = [string isEqualToString:filtered];
return basicTest;
}
}
You don't need to insert the name of the text field into the method. Instead do this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isEqual: txtCountry]) {
//return YES or NO here depending on your criteria
}
}

Restricting character input on iPhone UITextField [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
allow only alphanumeric characters for a UITextField
Is there a way of only letting the user input letters and numbers, no symbols, via the keyboard into a UITextField?
Use this delegate methods.. as It works for me..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == YOUR TEXT FIELD)
{
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:#"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:#""];
return [string isEqualToString:filtered];
}
else
return YES;
}
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
//return yes or no after comparing the characters
}
Try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if(![string stringByTrimmingCharactersInSet:nonNumberSet].length)
{
return NO;
}
return YES;
}

Remove the last character typed in UItextfield

I need to remove the last character if the textfield length exceeds 100,I used the following code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textfield.text.length>=100)
{
NSString *text;
textField.text=text;
NSString *newString = [text substringToIndex:[text length]-1];
textField.text=newString;
}
return YES;
}
But it just erases the whole text.
You erase the whole text because of this line:
textField.text=text; // text is nil here
What you wanted to do is more likely the following:
NSString *text = textField.text;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textfield.text.length>=100)
{
NSString *text;
text =[[textField text] stringByAppendingString:string];
NSString *newString = [text substringToIndex:[text length]-1];
textField.text=newString;
}
return YES;
}
Paste the code may solve your issue
In iOS 5 or later:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] >= 100)
{
[textField deleteBackward];
}
}
try with the below code
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText {
NSString* newText = [textView.text stringByReplacingCharactersInRange:aRange withString:aText];
// CGSize strSize = [newText sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:UILineBreakModeWordWrap];
if([newText length] > 100)
{
return NO; // can't enter more text
}
else
return YES; // let the textView know that it should handle the inserted text
}
try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] >100);
}
In order to fix the UITextField length you don't have to work with strings concatenation. Just use this simple code:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return !([string length]+[textField.text length] > FIXED_LENGTH);
}
This way you, as UITextFieldDelegate, are saying: "change the UITextField appending string to textField.text only if the sum of all characters doesn't exceed max length"
Here is an example how to remove any text in UITextField; applies to iOS 10.3 and later
#IBOutlet var upcText: UITextField!
if var textRange = upcText.selectedTextRange {
if textRange.isEmpty && textRange.start != upcText.beginningOfDocument {
// text has not been selected and the cursor is not at the beginnning
// then create new range to delete previous character
textRange = upcText.textRange(from: upcText.position(from: textRange.start, offset: -1)!, to: textRange.start)!
}
upcText.replace(textRange, withText: "")
}

shouldChangeCharactersInRange: method is not allowing to delete existing character

I am currently restricting the user to write in a text from more than 4 character.
For this i am using this piece of code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string{
if ([textField.text length] == 4)
return NO;
else
return YES;
}
Its working fine and it do not allow the user to write more than 4 character.
But when i try to delete the existing character.The keyboard delete option is not working.
How to solve this problem
When shouldChangeCharactersInRange method is called text field still has an old value and so if its length is 4 you're stuck. Better way to validate input will be to calculate what string you're going to have in a field after the change and check if it is valid:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range
withString:string];
return [newString length] <= 4;
}
write like below it will work
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength>4) ? NO : YES;
}

how to validate textfield that allows two digits after

i have to validate text field that allows only two digits after .
eg:12.34
if user enter more then two digits it won't allows text.
let me know is it understandable or no
how can i done,can any one please help me.
Thank u in advance.
You should code in the following delegate method,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSArray *sep = [string componentsSeparatedByString:#"."];
if([sep count]==2)
{
NSString *sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
if([sepStr length] >2)
//do whatever you want
}
}
You can also check if they use many decimal points by,
if([sep count]>2)
//use only one decimal point
I resolve my problem by using this code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:#"."];
if([sep count]>=2)
{
NSString *sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
return !([sepStr length]>2);
}
return YES;
}
Check the text validity in text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string