How enter only four value in text field from keyboard? - iphone

I am making an application in which i want to enter only numeric value. So i choose number pad type keyboard. Now i want that when user click on text filed and enter numeric value then enter only 0,1,2 and 3 not anything else. What i will do for that so i can apply restriction on text-field's value? And enter only single value not double value ie 11.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
}
Thanks in advances...

If rhe values are that restricted, you should try using a picker view or slider as the input view of the field.
How are you going to communicate to the user that 60% of the buttons on the numeric keypad are visible, enabled, tappable, but will do nothing?

in textField:shouldChangeCharactersInRange:replacementString: You can then check the character the user just entered for validity, returning NO for any invalid characters.
Here is a code sample to limit the size of a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[textField text] length] + [string length] - range.length > MAX_LENGTH) {
return NO;
} else {
return YES;
}
}
also see this

Related

Stop user from entering only spaces in textfield?

In my app I have a textfield in which users have to enter a string from 1-25 characters. The problem I'm having is stopping them from only entering spaces. I want them to be able to use spaces but only if they have other characters (abc, 1-9, etc.). How can I stop them from only entering spaces?
In your view controller set delegate of UITextField to self. Then implement following method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if([[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
return NO;
}
return YES;
}
Sounds like this question might help.
Just trim the whitespace off the string and then check to see if it is the empty string.
Perform a whitespace "trim" on their input and check to see if the result matches an empty string.

How to enable editing textfield with the delete feature working

I have a textfield, which can have at most only one character. When the focus is on the textfield,
(1) if the character count is more than 1, then the textfield shouldn't enable editing.
(2) But we should be able to delete that one character and replace it with another.
Right now I cant find a way to make both my conditions work. If I disable editing, the delete feature doesn't work. If I enable it, the textfield length condition is not satisfied.
You need to validate the change in text field delegate's textField:shouldChangeCharactersInRange:replacementString: method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return [newString length] <= 1;
}
Implement the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
and return NO if the range has location > 0 or length > 1.

Cococa Touch - Verifying each character inputed

I want to have the user type in something. And I want to verify each character inputed. How could I do this? I want it to be in real time. So as its typed its being verified and color coded.
You could compare what I want to do to an programmers IDE how it checks the syntax as you type it.
Any help is appreciated!
Connect the UITextField delegate to the app delegate and do something like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([string isEqual:#"b"]) {
// User typed 'b'
// Insert string with specific color
// [...]
} else {
// User typed something else
// Insert string without specific color
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
return YES;
}

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones keyboard.
is the answer to just run a regular expression on the input text and tell the user the filename is invalid (if so what would that regular expression be?)
ANSWER: (or what i ended up doing)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL valid;
//if the user has put in a space at the beginning
if ([string isEqualToString:#" "]){
if (range.location == 0){
valid = NO;
}
else{
valid = YES;
}
}
//otherwise test for alpha numeric
else{
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:#""];
}
//print the warning label
if (valid == NO){
[errorLabel setText:#"Invalid input"];
}
else{
[errorLabel setText:nil];
}
return valid;
}
You can implement the delegate method
For UITextField,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
For UITextview
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
and decide weather to append the entered characters or not.
You can implement the UITextFieldDelegate protocol and use textField:shouldChangeCharactersInRange:replacementString: to watch the text entry and prevent unwanted characters by returning NO.

Filtering characters entered into a UITextField

I have a UITextField in my application. I'd like to restrict the set of characters that can be can be entered into the field to a set that I have defined. I could filter the characters entered into the field when the text is committed using the UITextFieldDelegate method:
- (BOOL)textFieldShouldReturn:(UITextField*)textField
However, this gives the user a false impression as although restricted characters are removed from the final value, they were still visibly entered into the text field before pressing Return/Done/etc. What is the best approach that would prevent restricted characters appearing in the text field as they are selected on the keyboard?
Note: I am operating under the assumption that I have little control over which keys are provided by the iPhone keyboard(s). I am aware that I can switch between various keyboard implementations but am under the impression that I can't disable specific keys. This assumption may be incorrect.
I did as marcc suggested and it worked well. Sample implementation follows.
Note: Variable names were selected for brevity and do not reflect my coding standards:
...
myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"xyzXYZ"];
...
}
- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered {
for (int i = 0; i < [textEntered length]; i++) {
unichar c = [textEntered characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
return NO;
}
}
return YES;
}
Look at textField:shouldChangeCharactersInRange
This method is called by the UITextFieldDelegate whenever new characters are typed or existing characters are deleted from the text field. You could return NO to not allow the change.
Here is one of the cleanest approaches to restricting characters entered in a UITextField. This approach allows the use of multiple predefined NSCharacterSets.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
[allowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[allowedCharacters formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
if([string rangeOfCharacterFromSet:allowedCharacters.invertedSet].location == NSNotFound){
return YES;
}
return NO;
}
Look at the UITextViewDelegate method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string.
It's exactly what you need.
This is what I use to restrict the user to uppercase A-Z. Adjust the regex variable according to taste:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* regex = #"[^A-Z]";
return ([string rangeOfString: regex
options:NSRegularExpressionSearch].location == NSNotFound);
};
How about this?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* regex = #"[^a-z]";
return ([[string lowercaseString] rangeOfString: regex
options:NSRegularExpressionSearch].location == NSNotFound);
};
Note:
I am making all characters to lower case [string lowercaseString] so that you don't need to write in regex for captial/small letters.
You could loop and keep checking if the UITextField.text property has changed once the DidBeginEditing method gets called. If it has, check the text and remove an bad characters.