UITextField: backspace not working - iphone

I have an issue with UITextField's validation. I want to validate the UITextField's text-length to be 5(fixed).
my code :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField==codeTxt)
{
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet] ;
if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else if ([codeTxt.text length] >= 5])
return NO;
else
return YES;
}
}
this code works fine. It validates and ignores rest of the text(more thn 5).
my problem :
When I press Delete(Backspace), nothing happens !!! the text remains the same. delete(Backspace) does not work.
what could be the problem ?
Thanks...

put this condition at first statement in shouldChangeCharactersInRange method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string length]==0)
{
return YES;
}
if(textField==codeTxt)
{
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet] ;
if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else if ([codeTxt.text length] >= 5])
return NO;
else
return YES;
}
}

Your only checking the length of the current text in the field. This method gets called before the text changes, so you need to check the replacementString's length first, then check the textField's text length.
Try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == codeTxt) {
// NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
// if ([[string componentsSeparatedByCharactersInSet:[unacceptedInput count]] > 1) {
if ([[string componentsSeparatedByCharactersInSet:[[[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet] autorelease]] count] > 1) {
return NO;
} else if ([string length] < 5) {
return YES;
} else if ([codeTxt.text length] >= 5]) {
return NO;
} else {
return YES;
}
}
}
Edit:
Checking the textField's length probably isn't even necessary after doing [codeTxt.text length] >= 5] since this will prevent the textField's length from ever going above 4 anyway.
Actually, you would need to check it since the default is to return YES;.
Probably needs to be <= 5
} else if ([string length] <= 5) {
return YES;
}
Instead of < 5 too
} else if ([string length] < 5) {
return YES;
}

I got the solution
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField==codeTxt)
{
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet] ;
NSLog(#"textfield character validation method called");
NSLog(#"%d",[codeTxt.text length]);
if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else if ([codeTxt.text length] >= 5 && ![string isEqual:#""])
return NO;
else
return YES;
}
}

Related

iOS TextField Validation

I need a way to ensure that phone numbers have 10 digits with no other characters ie () - and make sure that email addresses are valid emails (formatted correctly).
Is there any library that can't make this easy for me so I don't have to write regular expressions.
This will check a UITextField for a proper email and phone number of 10 digits or less.
Add this method to the textFields delegate then check if the characters it is about to change should be added or not.
Return YES or NO depending on the text field, how many characters are currently in it, and what characters it wants to add:
#define ALPHA #"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMERIC #"1234567890"
#define ALPHA_NUMERIC ALPHA NUMERIC
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *unacceptedInput = nil;
switch (textField.tag) {
// Assuming EMAIL_TextField.tag == 1001
case 1001:
if ([[textField.text componentsSeparatedByString:#"#"] count] > 1)
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:#".-"]] invertedSet];
else
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:#".!#$%&'*+-/=?^_`{|}~#"]] invertedSet];
break;
// Assuming PHONE_textField.tag == 1002
case 1002:
if (textField.text.length + string.length > 10) {
return NO;
}
unacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
break;
default:
unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
break;
}
return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}
Also, check out these 2 articles:
Auto-formatting phone number UITextField on the iPhone
PhoneNumberFormatter.
Here's a simple way of ensuring phonenumber length in the UIViewController that has the text field in it's view.
- (void)valueChanged:(id)sender
{
if ([[[self phoneNumberField] text] length] > 10) {
[[self phoneNumberField] setText:[[[self phoneNumberField] text]
substringToIndex:10]];
}
}
- (void) viewWillAppear:(BOOL)animated
{
[[self phoneNumberField] addTarget:self
action:#selector(valueChanged:)
forControlEvents:UIControlEventEditingChanged];
}
For emails I suppose you want to check against a regexp when it loses focus.
Here's Simple Example for UITextField Validation while type in keyboard other characters not displaying
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//UITextField *tf_phonenumber,*tf_userid;
if (textField.text<=10) {
char c=*[string UTF8String];
if (tf_phonenumber==textField) //PhoneNumber /Mobile Number
{
if ((c>='0' && c<='9')||(c==nil)) {
return YES;
}
else
return NO;
}
if (tf_userid==textField) //UserID validation
{
if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c==' ')||(c==nil)) {
return YES;
}
else
return NO;
}
return YES;
}
else{
return NO;
}
}

TextField:shouldChangeCharactersInRange:replacementString: return trap!

Im calling UITableViewCell delegate method TextField:shouldChangeCharactersInRange:replacementString: on a custom cell that has four UITextFields and what is happening once maxlength is reached on one of the fields it dose not let you enter any text in the other fields because its always returning "no" for that initial if statement thats being satisfied. any ideas on how to get around this?
I'm only testing on two fields at the moment. Thank you for any help in advance.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int regFieldOnelength = [regFieldOne.text length] ;
int regFieldTwolength = [regFieldTwo.text length] ;
if ((regFieldOnelength >= MAXLENGTH && ![string isEqualToString:#""]) || (regFieldTwolength >= MAXLENGTH && ![string isEqualToString:#""])) {
if(regFieldOne.text = [regFieldOne.text substringToIndex:MAXLENGTH]){
return NO;
}
if(regFieldTwo.text = [regFieldTwo.text substringToIndex:MAXLENGTH]){
return NO;
}
}
return YES;
}
Why are you using booth the text field, Instead use the current text field that is being edited
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int currentTxtLen = [textField.text length] ;
if (currentTxtLen >= MAXLENGTH && ![string isEqualToString:#""]) {
return NO;
}
return YES;
}
Can you try
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int regFieldOnelength = [regFieldOne.text length] ;
int regFieldTwolength = [regFieldTwo.text length] ;
if((textField == regFieldOne) ||(textField == regFieldTwo)) {
if ((regFieldOnelength >= MAXLENGTH && ![string isEqualToString:#""]) || (regFieldTwolength >= MAXLENGTH && ![string isEqualToString:#""])) {
if(regFieldOne.text = [regFieldOne.text substringToIndex:MAXLENGTH]){
return NO;
}
if(regFieldTwo.text = [regFieldTwo.text substringToIndex:MAXLENGTH]){
return NO;
}
}
return YES;
}
else {
return YES;
}
}

Validate against empty UITextField?

What is the value of a UITextField when it is empty? I can't seem to get this right.
I've tried (where `phraseBox' it the name of the said UITextField
if(phraseBox.text != #""){
and
if(phraseBox.text != nil){
What am I missing?
// Check to see if it's blank
if([phraseBox.text isEqualToString:#""]) {
// There's no text in the box.
}
// Check to see if it's NOT blank
if(![phraseBox.text isEqualToString:#""]) {
// There's text in the box.
}
found this at apple discussions when searching for the same thing,thought ill post it here too.
check the length of the string :
NSString *value = textField.text;
if([value length] == 0) {
}
or optionally trim whitespaces from it before validation,so user cannot enter spaces instead.works well for usernames.
NSString *value = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if([value length] == 0) {
// Alert the user they forgot something
}
Try following code
textField.text is a string value so we are checking it like this
if([txtPhraseBox.text isEqualToString:#""])
{
// There's no text in the box.
}
else
{
NSLog(#"Text Field Text == : %# ",txtPhraseBox.text);
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *fullText = [textField.text stringByAppendingString:string];
if ((range.location == 0) && [self isABackSpace:string]) {
//the textFiled will be empty
}
return YES;
}
-(BOOL)isABackSpace:(NSString*)string {
NSString* check =#"Check";
check = [check stringByAppendingString:string];
if ([check isEqualToString:#"Check"]) {
return YES;
}
return NO;
}
Use for text field validation:
-(BOOL)validation{
if ([emailtextfield.text length] <= 0) {
[UIAlertView showAlertViewWithTitle:AlertTitle message:AlertWhenemailblank];
return NO; }
return YES;}
Actually, I ran into slight problems using Raphael's approach with multiple text fields. Here's what I came up with:
if ((usernameTextField.text.length > 0) && (passwordTextField.text.length > 0)) {
loginButton.enabled = YES;
} else {
loginButton.enabled = NO;
}
Validation against empty UITextfield. if you don't want that UITextField should not accept blank white spaces. Use this code snippet:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound) {
return YES;
} else {
return NO;
}
}

Iphone UITextField only integer

I have a UITextField in my IB and I want to check out if the user entered only numbers (no char)and get the integer value.
I get the integer value of the UITextField like that :
int integer = [myUITexrtField.text intValue];
When I put a character ( , ; . ) it return me 0 and I don't know how to detect that it is not only numbers.
How can I do?
Implementing shouldChangeCharactersInRange method as below does not allow the user input non-numeric characters.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:#""];
}
This returns YES if the string is numeric, NO otherwise. the [string isEqualToString#""] is to support the backspace key to delete.
I love this approach because it's clean.
Be sure to set your text field delegate
Use the following function to ensure user can type in numbers only:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber* candidateNumber;
NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];
range = NSMakeRange(0, [candidateString length]);
[numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];
if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {
return NO;
}
else
{
return YES;
}
}
Try this: It will stop user to enter any character other then numbers
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound)
{
return NO;
}
return YES;
}
http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Objective-C
Or you could ensure that only the numeric keyboard appears when the focus comes on the field
To only allow for numeric input:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return [string isEqualToString:#""] ||
([string stringByTrimmingCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].length > 0);
}
To test for an integer:
- (BOOL)isNumeric:(NSString *)input {
for (int i = 0; i < [input length]; i++) {
char c = [input characterAtIndex:i];
// Allow a leading '-' for negative integers
if (!((c == '-' && i == 0) || (c >= '0' && c <= '9'))) {
return NO;
}
}
return YES;
}
Answer by #devsan is incorrect. If a user pastes anything with a a number such as "1abc" her code would break
It should be (allow replacement only if all the chars are digits):
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length == string.length) || [string isEqualToString:#""];
}
You could also use UITextFieldDelegate method
textField:shouldChangeCharactersInRange:replacementString:
to live check that each time the user press a key, it is a simple digit, so that he/she knows that only int value is to be entered in the field.
Want to allow negative numbers as well?
Elaborating on davsan's answer, here is a solution that supports entering negative as well as positive numbers, and disallows anything else.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:#""] || ([textField.text isEqualToString:#""] && [string isEqualToString:#"-"]);
return YES;
}
This code is work with localisation too .
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#""]) return YES;
NSCharacterSet *characterSet = nil;
characterSet = [NSCharacterSet decimalDigitCharacterSet];
NSRange location = [string rangeOfCharacterFromSet:characterSet];
return (location.location != NSNotFound);
if(characterSet == nil) return YES;
return YES;
}

Set UITextField Maximum Length [duplicate]

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