How to know if a UITextField in iOS has blank spaces

前端 未结 9 695
一整个雨季
一整个雨季 2020-12-01 03:33

I have a UITextField where user can enter a name and save it. But, user should not be allowed to enter blank spaces in the textFiled.

1 - How can I find out,

相关标签:
9条回答
  • 2020-12-01 04:08

    Here's what I did using stringByReplacingOccurrencesOfString.

    - (BOOL)validateFields
     {
          NSString *temp = [textField.text stringByReplacingOccurrencesOfString:@" "
                                                              withString:@""
                                                                 options:NSLiteralSearch
                                                                   range:NSMakeRange(0, textField.text.length)];
    
          if ([temp length] == 0) {
              // Alert view with message @"Please enter something."
              return NO;
          }
     }
    
    0 讨论(0)
  • 2020-12-01 04:11

    if you really want to 'restrict' user from entering white space

    you can implement the following method in UITextFieldDelegate

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

    If user enter space in the field, there is no change in the current text

    0 讨论(0)
  • 2020-12-01 04:11

    @Hanon's answer is the pretty neat, but what I needed was to allow at least 1 white space, so based on Hanon's solution I made this one:

    I declared a local variable called whitespaceCount to keep the counts of the white spaces. Hope this helps anybody!

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string 
    { 
        NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
    
        if ([string rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) 
        {
            whitespaceCount++;
            if (whitespaceCount > 1) 
            {
                return NO;
            }
        }
        else 
        {
            whitespaceCount = 0;
            return YES;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 04:19

    I had a same condition not allowing user to input blank field

    Here is my code and check statement

    - (IBAction)acceptButtonClicked:(UIButton *)sender {
    if ([self textFieldBlankorNot:fullNametext]) {
            fullNametext.text=@"na";
        }
    // saving value to dictionary and sending to server
    
    }
    
    -(BOOL)textFieldBlankorNot:(UITextField *)textfield{
        NSString *rawString = [textfield text];
        NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
        if ([trimmed length] == 0)
            return YES;
        else
            return NO;
    }
    
    0 讨论(0)
  • 2020-12-01 04:20

    You can "trim" the text, that is remove all the whitespace at the start and end. If all that's left is an empty string, then only whitespace (or nothing) was entered.

    NSString *rawString = [textField text];
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
    if ([trimmed length] == 0) {
        // Text was empty or only whitespace.
    }
    

    If you want to check whether there is any whitespace (anywhere in the text), you can do it like this:

    NSRange range = [rawString rangeOfCharacterFromSet:whitespace];
    if (range.location != NSNotFound) {
        // There is whitespace.
    }
    

    If you want to prevent the user from entering whitespace at all, see @Hanon's solution.

    0 讨论(0)
  • 2020-12-01 04:22

    UPD: Swift 2.0 Support

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let whitespaceSet = NSCharacterSet.whitespaceCharacterSet()
        let range = string.rangeOfCharacterFromSet(whitespaceSet)
        if let _ = range {
            return false
        }
        else {
            return true
        }
    }
    
    0 讨论(0)
提交回复
热议问题