iPhone Keyboard Filter Question - iphone

I want to limit the character users can type in iPhone's keyboard, so I created an array of my own. e.g. The array including 0~9 and a dot to enable users to type a price. Then I can return NO for -(BOOL)textField:shouldChangeCharactersInRange:replacementString: if the replace string is not in the array.
The problem is that the backspace button is also disabled when I use this array to filter text. Any ideas about how to enable backspace button?
Another problem is that I want to let users type their names and therefore I don't want to let them switch to numbers and punctuaction (backspace button is also locked if I use an array to filter). How to disable the switch button on the keyboard (Now I just limit them to type a~z, blank and "." , but I think disable the switch button might be a better way)?

I find a way (maybe not good enough, but it can do the work for backspace function):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == txtChargeAmt)
{
if(string.length == 0) //backspace button is pressed
{
textField.text = [textField.text substringToIndex:(textField.text.length - 1)];
return NO;
}
for(NSString *s in arrNumberAndDot)
{
if([string isEqualToString:s])
{
return YES;
}
}
return NO;
}
else
return YES;
}
Other ideas about the backspace issue are welcomed. And how to disable the switch button then?

I guess I am not sure why you would want to use the same TextField for these two different types of input.
I would have two fields, an alphanumeric field for name entry and a numeric field for number entry.
Or am I not getting your question?

This will do what you want a little more succinctly (and efficiently):
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
NSCharacterSet *validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#".0123456789"];
BOOL shouldChange =
[string length] == 0 || // deletion
textField != txtChargeAmt || // not the field we care about
[string rangeOfCharacterFromSet:validCharacterSet].location != NSNotFound;
if (!shouldChange)
{
// Tell the user they did something wrong. There's no NSBeep()
// on the iPhone :(
}
return shouldChange;
}
I'd construct that character set somewhere else so you you'd only have to do it once, but you get the idea. Anyone have any thoughts on what to do to alert the user they used an invalid character? I'm trying to solve a similar problem.

Related

How to check that any of the textfields inputed values is not number and is negative then show message

I have many textField Inputs for the calculations I want that if any of the inputs is not a number and negative number then it show error message on button click and not to move to the other screen
There's at least a couple ways you can handle this, James.
#1) specify numeric keyboards for those specific numeric-only text fields in the storyboard / xib file
#2) set a delegate for the text field and when the user is done editing (e.g. textFieldDidEndEditing:), look at the contents of the text field and if you see anything that isn't a number, throw up a UIAlert.
One way to do this would be:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSRange rangeOfInvalidCharacter = [textField.text rangeOfCharacterFromSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
if(rangeOfInvalidCharacter.location != NSNotFound)
{
// throw up a UIAlert here
// and, if you want, erase the bogus text via:
textField.text = #"";
return;
}
// and if we get here, that means the text field contents are only digits.
}
Try this code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
{
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
return NO; // For don't add this character
}
return YES; // For input a true character
}

4 digit password type view: number and cursor control

First posting to Stackoverflow, but have been searching for answers for sometime now.
Learning Objective-C & XCode and have been creating simple projects.
Currently, I wanted to attempt a screen where user will enter in a 4 digit password code, where each individual digit in seperate text fields.
1) How to make cursor be in first text field, causing keyboard to come up automatically when app is started
2) How to make the text field only accept one digit. Can you do this in interface builder, in the attributes for the text field? Or do you have to do that programmatically?
3) How to make cursor jump to next text field after previous one is filled. Will this happen automatically when question #2 is done?
Would appreciate any help... thanks!
Ok here you go
1. You must create 4 UITextFields in the IB say textField1, textField2, textField3, textField4. and call [textField1 becomesFirstResponder]; this will make the cursor in your textfield1.
2.To make textfield accept only one digit, you will have to do this
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
textField.text = string;
if ([string length] > 0) {
if ([textField isEqual:textField1]) {
[textField2 becomeFirstResponder];
}else if([textField isEqual:textField2]) {
[textField3 becomeFirstResponder];
}else if([textField isEqual:textField3]) {
[textField4 becomeFirstResponder];
}else if([textField isEqual:textField4]) {
[textField resignFirstResponder];
}
}
return FALSE;
}
you will have to do this programmatically.
3.same as ans2.
Create a hidden uitextfield that you are actually typing into. Then create four 'seen' uitextfields and add a bullet character into each one as the user types into the hidden field.
This is the easiest way to do what you want.

Disable a particular key in UI Keyboard

It need to disable '&' key from number and punctuation keyboard, so is it possible to disable a particular key in UIKeyboard?
I don't think it's possible to disable a certain key (unless it's one of the action keys such as the return key) but if you are using a UITextField you can use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string delegate method to see if the user pressed the & key and remove it from the string
You cannot do that. However your options are:
create your own custom keyboard not offerring '&' key (too much effort IMO)
If you use UITextField you can validate the text submitted by user: remove '&' and/or inform user that it is not allowed to use '&' (much easier).
EDIT: you can also connect UITextField's "Editing Changed" event to the File's Owner's IBAction and filter out '&' there.
There is one delegate method for textField in which you can block specific characters if you want based on their ASCII values. The method can be written as follows:
-(BOOL)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b
{
char s=[i characterAtIndex:0];
if(selTextField.tag==1)
{
if(s>=48 && s<=57 && s == 38) // 48 to 57 are the numbers and 38 is the '&' symbol
{
return YES;
}
else
{
return NO;
}
}
}
This method will permit only numbers and & symbol to be entered by the user. Even if the user presses other characters they won't be entered. And as it is a textField's delegate method you don't need to worry about calling it explicitly.
//Disabling the '<' '>' special characters key in Keyboard in my code
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSCharacterSet *nonNumberSet = [NSCharacterSet characterSetWithCharactersInString:#"<>"];
if (range.length == 1)
return YES;
else
return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
return YES;
}

How to test a UITextField for being nil?

I am trying to make a part of my app where if the person doesn't change the blank text in my UITextField, then he/she can't go on to the next step. Basically, I want to test the UITextField for nil text. I have used the if (text == #"") method, but if the person clicks on the UITextField but doesn't type, then the if statement doesn't work. For some reason it doesn't think the text == nil or "". Am I implementing the code wrong. Any other options. Please help!!!
You should be checking the length of the text property:
if([[textField text] length] == 0) {
//do something...
}
Here's the category I use...
#implementation NSString (NSString+Extensions)
- (BOOL)isNotBlank {
return [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0;
}
#end
This way a nil string would evaluate to false, which is correct. Creating an isBlank would return false for nil, which isn't correct.
I have write code to check the string is empty or not. This code also check for the string only space that is also empty for store name and address etc. this will help you.
NSString *stringTemp = textField.text;
stringTemp = [stringTemp stringByReplacingOccurrencesOfString:#" " withString:#""];
if ([stringTemp isEqualToString:#""]) {
NSLog(#"Empty string");
}
else{
NSLog(#"string has some content ");
}
Thanks
If I were you I would disable and enable the button while the user is typing. Imho it's better that the button looks disabled when there is no text than having the user click the button to tell him that he is not allowed to move to the next view. Most of apples own apps do it like this.
You achieve this behavior by using the UITextFieldDelegate method like this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// "Length of existing text" - "Length of replaced text" + "Length of replacement text"
NSInteger textLength = [aTextView.text length] - range.length + [text length];
if (textLength > 0) {
doneButton.enabled = YES;
}
else {
doneButton.enabled = NO;
}
return YES;
}
If you provide a prefilled textfield you have to enable the button in viewDidLoad (or where ever you want) and if you provide an empty field you have to disable it initally.

UIKeyboard turn Caps Lock on

I need my user to input some data like DF-DJSL so I put this in the code:
theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
But unfortunately what happens is the first to letter type in CAPS but then letter immediately after typing the hyphen will be in lower case and then the rest return to CAPS therefore producing output like this (unless the user manually taps the shift button after typing a hyphen): DF-dJSL
How can I fix this?
Many Thanks
You don't mention which SDK you're using, but against 3.0 and above I see your desired behaviour.
That said, you could always change the text to upper case when they finish editing using the textFieldDidEndEditing method from the delegate:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *textToUpper = [textField.text uppercaseString];
[theTextField setText:textToUpper];
}
Or, by setting up a notification on the textfield when it changes, you could change the text as it is being typed:
// setup the UITextField
{
theTextField.delegate = self;
theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
[theTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
You have to do it this way since, unlike UISearchBar, UITextField doesn't implement textDidChange. Something like this, perhaps?
- (void)textFieldDidChange:(UITextField *)textField {
NSRange range = [textField.text rangeOfString : #"-"];
if (range.location != NSNotFound) {
theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
}
}