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;
}
Related
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;
}
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
}
}
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
This question already has answers here:
Set the maximum character length of a UITextField
(46 answers)
Closed 10 years ago.
Is there any way to set the maximum length on a UITextField?
Something like the MAXLENGTH attribute in HTML input fields.
This works correctly with backspace and copy & paste:
#define MAXLENGTH 10
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
BOOL returnKey = [string rangeOfString: #"\n"].location != NSNotFound;
return newLength <= MAXLENGTH || returnKey;
}
UPDATE: Updated to accept the return key even when at MAXLENGTH. Thanks Mr Rogers!
UPDATE
I cannot delete this answer because it is the accepted one, but it was not correct. Here is the correct code, copied from TomA below:
#define MAXLENGTH 10
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
BOOL returnKey = [string rangeOfString: #"\n"].location != NSNotFound;
return newLength <= MAXLENGTH || returnKey;
}
ORIGINAL
I think you mean UITextField. If yes, then there is a simple way.
Implement the UITextFieldDelegate protocol
Implement the textField:shouldChangeCharactersInRange:replacementString: method.
That method gets called on every character tap or previous character replacement. in this method, you can do something like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > MAXLENGTH) {
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
return NO;
}
return YES;
}
A better function which handles backspaces correctly and limits the characters to the supplied length limit is the following:
#define MAXLENGTH 8
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int length = [textField.text length] ;
if (length >= MAXLENGTH && ![string isEqualToString:#""]) {
textField.text = [textField.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
Cheers!
This code limits the text while also allowing you enter characters or paste anywhere into the text. If the resulting text would be too long it changes the characters in the range and truncates the resulting text to the limit.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] - range.length + [string length];
if (newLength >= MAXLENGTH) {
textField.text = [[textField.text stringByReplacingCharactersInRange:range withString:string] substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
I think this code would do the trick:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString*)string
{
if (range.location >= MAX_LENGTH)
return NO;
return YES;
}
With this delegate method you can prevent the user to add more characters than MAX_LENGTH to your text field and the user should be allowed to enter backspaces if needed.
For me this did the magic:
if (textField.text.length >= 10 && range.length == 0)
return NO;
return YES;
this is how i resolved that problem. When max limit is reached it wont try to add more... you will only be able to remove chars
#define MAX_SIZE ((int) 5)
...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] >= MAX_SIZE && ![string isEqualToString:#""]) {
return NO;
}
return YES;
}
I think there's no such property.
But the text you assign to the UILabel has to be an NSString. And before you assign this string to the UILabel's text property you can for example use the following method of NSString to crop the string at a given index (your maxlength):
- (NSString *)substringToIndex:(NSUInteger)anIndex
This is similar to coneybeare's answer, but now the text field can contain a maximum of MAXLENGTH symbols:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > MAXLENGTH - 1) {
textField.text = [textField.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
You have to be aware of the location the text is placed in as well as the length of text being added (in case they're pasting more than one character). The pattern between these with respect to max length is that their sum should never exceed the max length.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSInteger locationAndStringLengthSum = range.location + [string length];
if ([textField isEqual:_expirationMonthField]) {
if (locationAndStringLengthSum > EXP_MONTH_FIELD_MAX_CHAR_LENGTH) {
return NO;
}
}
else if ([textField isEqual:_expirationYearField]) {
if (locationAndStringLengthSum > EXP_YEAR_FIELD_MAX_CHAR_LENGTH) {
return NO;
}
}
else if ([textField isEqual:_securityCodeField]) {
if (locationAndStringLengthSum > SECURITY_FIELD_MAX_CHAR_LENGTH) {
return NO;
}
}
else if ([textField isEqual:_zipCodeField]) {
if (locationAndStringLengthSum > ZIP_CODE_MAX_CHAR_LENGTH) {
return NO;
}
}
return YES;
}
You need to assign delegate on ViewDidLoad
TextFieldname.delegate=self
I have a UITextField.
How can I make sure that the first character is uppercase?
I tried this.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength == 1) {
string = [string uppercaseString];
}
return YES;
}
But even after uppercasing the string, it is still lower case.
Thanks,
Tee
Ah I got it.
There is the capitalize option in Interface Builder.
Thanks,
Tee