Text Validation - user can only select a number once - iphone

I am trying to create a questionnaire, in which the user shall rate statements from 1-7.
The user rates the statements by filling UITextFields with a value between 1-7.
I have made it so that the user can only write in values between 1-7 and that they only can write one character per textfield.
What I want to do now is to prevent the user from using that value more than once.
This is the code that I have so far.
#define CHARACTERS #"1234567"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] >1 ) | ([newString length] > 1) )
return NO;
else
return YES;
}

I would use a dictionary. Whenever a number is chosen in a textfield, check if the dictionary has that value. If it doesn't have the value, the value entered is correct. Add the entry into the dictionary now and then let the user proceed to the next textfield. If you repeat this process for all the textfields you will be able to solve this problem pretty easily.

You can do one thing give tag to all textfields and check whether string you entered in current textfield is same as text of all other textfields then return NO.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] >1 ) | ([newString length] > 1) )
return NO;
else
//Call function to check if string is same as other textfield.text then return NO else YES
}
else
another option is you can take 1 string and save textfield.text in that string and update all time when you enter and check with it whether the string you are entering is in substring of that string.
best of luck

At the time of creating UITextField set the keyboardType as UIKeyboardTypeNumberPad.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text]length]==1)
//perform condition
else
//perform condition
}
this might help

Related

Limiting the user to enter the characters and also to convert the each entered letter into capital letter in ios

I am having 4 text fields in which I want to put two limitations simultaneously. One is that the user should be able to type in only capital letters with the maximum character limit to 2 only. My code for this is as follows:-
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
// Below logic is for All 4 Modifer Textfields
// we are restrict the user to enter only max 2 characters in modifier textfields.
if (textField==txt_modifier1 || textField==txt_modifier2 || textField==txt_modifier3 ||
textField==txt_modifier4) {
textField.text = [textField.text stringByReplacingCharactersInRange:range
withString:[string
uppercaseStringWithLocale:[NSLocale currentLocale]]];
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 2) ? NO : YES;
}
return YES;
}
this is not functioning properly as it is appending one more character as I type in any character and also not limiting the number of characters to 2. Please suggest a way to tackle this problem.
You're manually updating the text in your textfield and then sending YES back (which then appends the characters a second time). Then, you're usingthe new text with the replacement string to compare it to two (which, then appends your characters again) ...
Try this:
if (...) {
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
if (result.length <= 2)
textField.text = result;
return NO;
}
return YES;
For limiting the number of characters and capitalizing it in your UITextField, use this code block
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
textField.text = [textField.text capitalizedString];
if(textField.text.length >= 3 && range.length == 0)
{
return NO;
}
else
{
return YES;
}
Please try to use this one ...It may help you
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:[string capitalizedString]])
{
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
if (result.length <= 2)
textField.text = result;
return NO;
}
else
return YES;
}
loginUITextFieldTextDidChangeNotificationMake judgment here
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(textChanged:)
name:UITextFieldTextDidChangeNotification
object:YOUR_TEXT_FIELD];
-(void)textChanged:(NSNotification *)notif {
//to do your logic
}

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
}
}

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;
}

Run time validation in UItextField

I want to have runtime validation on my UITextField, i.e. when user starts entering data, it should validate and pop up error message if not correct. How do I implement this feature ?
textfieldDidChange or textFieldShouldEndEditing can be of any help ?
Any tutorial ?
You can implement the textField:shouldChangeCharactersInRange:replacementString: method on your text field's delegate, rejecting any characters that are invalid for your textfield. See the UITextFieldDelegate protocol reference for more information.
For example, if you only want to allow entry of decimal numbers, you could use:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
This won't actually pop up an error message if the input is incorrect; instead, it will prevent entry of characters that are incorrect.
If you do want to allow the user to type invalid characters, and them give them an error, you could implement this in the textFieldShouldEndEditing: method of your delegate.
This is a better solution since it allows backspaces.
Taken from :Hugo Larcher's blog
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet;
if (textField == self.priceField) //allow decimals
nonNumberSet=[[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
else
nonNumberSet=[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
// allow backspace
if (range.length > 0 && [string length] == 0) {
return YES;
}
// do not allow . at the beggining
if (range.location == 0 && [string isEqualToString:#"."]) {
return NO;
}
// set the text field value manually
NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:#""];
textField.text = newValue;
// return NO because we're manually setting the value
return NO;
}

how to validate a Text field to accept only Characters at run Time in iphone

i want to validate a text field to accept only characters while typing in it.....
I use the following for integers but I guess you could easily modify it to scan for strings/chars (see NSScanner):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
// This allows backspace
if ([resultingString length] == 0) {
return true;
}
NSUInteger holder;
NSScanner *scan = [NSScanner scannerWithString: resultingString];
return [scan scanInteger: &holder] && [scan isAtEnd];
}
Don't forget to set the UITextField delegate appropriately :)