I can't seem to figure out the best way to handle this. When user presses decimal point I want it to move to next textfield. The problem I'm having is it's displaying the decimal in the next text field and only works if I type a decimal only. I believe I understand why it's only allowing decimal is because of the way i'm starting if statement, but I can't seem to figure out the best approach here.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *decmial = #".";
if ([textField.text isEqualToString:#"."])
{
if (octet1.text == decmial) {
octet1.text = [octet1.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet1 isFirstResponder];
[octet2 becomeFirstResponder];
}
else if (octet2.text == decmial) {
octet2.text = [octet2.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet2 isFirstResponder];
[octet3 becomeFirstResponder];
}
else if (octet3.text == decmial) {
octet3.text = [octet3.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet3 isFirstResponder];
[octet4 becomeFirstResponder];
}
else if (octet4.text == decmial) {
octet4.text = [octet4.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet4 isFirstResponder];
[myBitMask becomeFirstResponder];
}
}
return YES;
}
Currently, you're only doing anything if the entire content of the text field being modified is exactly ".". I assume that you should really be testing to see if the user has typed that in instead, by comparing with the replacement string:
if ([string isEqualToString:#"."]) {
You then go on to test each of your text fields in turn, without any regard for the actual text field being modified (the one passed into the function). You try to check each one to see if it's exactly "." (except you don't, because you're using == instead of isEqualToString), and if it is then you replace that entire string by what ever the user's just typed. Very odd indeed!
Based on your description of what yo want to do, how about:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *decmial = #".";
if ([string isEqualToString:decmial])
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:decmial withString:#""];
if (textField == octet1) {
[octet2 becomeFirstResponder];
}
else if (textField == octet2) {
[octet3 becomeFirstResponder];
}
else if (textField == octet3) {
[octet4 becomeFirstResponder];
}
else if (textField == octet4) {
[myBitMask becomeFirstResponder];
}
// We've done the replacement ourselves and moved to the next field. Don't allow the system to accept the change
return NO;
}
// Default behavior
return YES;
}
Related
I am implementing a textfield to input user passcode. I am trying to match the length of stored passcode with user entered passcode. When the length matches i try to validate passcode. If the passcode does not match or length gets greater than stored value then i try to clear the textfield (the else-if) but the last entered character still stays in text field.
Please propose what i am doing wrong or what is the proper way?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
enteredPasscode = [enteredPasscode stringByAppendingString:string];
if(enteredPasscode.length == [[self.passcodeFromDB stringValue] length])
{
[self performMatch:enteredPasscode];
}
else if(enteredPasscode.length > [[self.passcodeFromDB stringValue] length]){
self.passcodeField.text = #"";
textField.text = #"";
enteredPasscode = #"";
}
return YES;
}
you need to return NO; when you set everything to #"". I just checked and it works for me
I'm using rangeofString and textfield: shouldChangeCharactersinRange: to restrict the types of keystrokes that will be valid in a textfield.
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
this works fine EXCEPT i now can't use the delete key. Any ideas how to represent the delete key to add it to the includeString?
I tried
`NSString *includeString = #"1234567890-()+\b"
but that didn't work - neither did it allow the \ or b characters to appear which i thought odd
Thanks
The replacement string string is empty when characters are deleted.
Since rangeOfString:string returns NSNotFound for an empty string,
you have to check for that situation first:
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string length] == 0)
return YES;
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
Update: As #rmaddy correctly pointed out, the above method fails if more than one
character is pasted into the text field. The following method checks if all
characters of the replacement string are valid. (There are probably many solutions,
this is only one of them.)
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
static NSString *includeString = #"1234567890-()+";
NSCharacterSet *includeSet = [NSCharacterSet characterSetWithCharactersInString:includeString];
if ([[string stringByTrimmingCharactersInSet:includeSet] length] > 0)
return NO;
return YES;
}
Note that the empty string does not need to be handled separately anymore.
I would like to prevent the user from not entering any data as well as entering only spaces. So basically there must at least be one character without a space. Then I would also like to remove any spaces at the beginning of the word so the first letter is a character that is not a space.
edit
the user must enter something and if the user enters a few spaces before it then I want to trim those spaces. I also want to prevent the user from just entering spaces.
Example
if the user enter's a name and surname like " James Dean" I would like to take the first space away but not the second space between James and Dean.
Set your UIViewController to be your destination UITextField's delegate and implement this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// verify the text field you wanna validate
if (textField == _nameTextField) {
// do not allow the first character to be space | do not allow more than one space
if ([string isEqualToString:#" "]) {
if (!textField.text.length)
return NO;
if ([[textField.text stringByReplacingCharactersInRange:range withString:string] rangeOfString:#" "].length)
return NO;
}
// allow backspace
if ([textField.text stringByReplacingCharactersInRange:range withString:string].length < textField.text.length) {
return YES;
}
// in case you need to limit the max number of characters
if ([textField.text stringByReplacingCharactersInRange:range withString:string].length > 30) {
return NO;
}
// limit the input to only the stuff in this character set, so no emoji or cirylic or any other insane characters
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "];
if ([string rangeOfCharacterFromSet:set].location == NSNotFound) {
return NO;
}
}
return YES;
}
try like this may be it helps to you,here is my code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
if([text.text length]==0){
if([string isEqualToString:#" "]){
return NO;
}
}
return YES;
}
By placing this code user won't enter space as a first letter but it accepts the space in the middle of the string.
I'll give you a hint for the first part.
NSString *tempname1 = [self.textField.text stringByReplacingOccurrencesOfString:#" " withString:#""];
BOOL thereAreJustSpaces = [tempname1 isEqualToString:#""];
-(void)removeSpacesFromTextFields:(id) sender {
NSString *trim = [self.FNTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([trim length] == 0) {
self.FNTextField.text = #"";
}
}
Try this, If you want to prevent space in your text field.
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(checkSpace:) name:UITextFieldTextDidChangeNotification object:textfield];
}
-(void)checkSpace:(NSNotification *)notification
{
str = [textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
textfield.text=str;
}
I am using BSKeyboard Controls to have a next/previous and done button above the keyboard on login fields for username and password.
What I want to achieve is:
- When one of the fields is blank the done button should say 'Done'
- When both fields have at least one character it should say 'Login'
I understand there are multiple ways to check a textfield contents, hasText isEqualToString !=nil etc. But I am trying to check characters here I guess.
I need to know what would be the best location to put an if statement and which to use.
My fields are
self.usernameField
self.passwordField
My Keyboard control is updated like this:
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsDone", #"test");
OR
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsLogin", #"test");
Updated Method:
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
UITextField *otherTextField;
if (textField == self.passwordField)
{
otherTextField = self.usernameField;
}
else
{
otherTextField = self.passwordField;
}
if ([newText length] > 0 && [otherTextField.text length] > 0)
{
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsLogin",#"Button for Keyboard Controls on Login page");
} else {
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsDone", #"test");
}
You can implement textField:shouldChangeCharactersInRange:replacementString: of UITextFieldDelegate to execute whatever you want when the user enters a key.
Something like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
UITextField *otherTextField;
if (textField == self.passwordField)
{
otherTextField = self.usernameField;
}
else
{
otherTextField = self.passwordField;
}
if ([newText length] > 0 && [otherTextField.text length] > 0)
{
// Your code
}
return YES;
}
EDIT
Instead of using that delegate method,use the event editing changed. You have to set the action for that event with IB or by code, and it would look like this:
- (IBAction) textFieldEditingChanged
{
if ([self.usernameField.text length] > 0 && [self.passwordField.text length] > 0)
{
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsLogin",#"Button for Keyboard Controls on Login page");
} else {
self.keyboardControls.doneTitle = NSLocalizedString(#"KeyboardControlsDone", #"test");
}
}
I am trying to get the URL and google search in the same Text field.. the method I am using works alright but probably there is a better way. What I am doing is to check if there is a dot in the input like www.google.com, if a dot is not found, then search it on google..
NSRange range = [textField.text rangeOfString:#"."];
textField.text = (range.location != NSNotFound) ?
[NSString stringWithFormat:#"%#", textField.text] :
[#"http://www.google.com/search?q=" stringByAppendingString:textField.text ];
If the input has a dot, then search fails.. Is there a better way to do it? Thanks..
U can prevent user from entering . dot in UITextField like this by using UITextField's delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#"."])
{
return NO;
}
else
{
return YES;
}
}