How to know if a UITextField in iOS has blank spaces

32,344

Solution 1

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.

Solution 2

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

Solution 3

Use following lines of code

NSString *str_test = @"Example ";
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if([str_test rangeOfCharacterFromSet:whitespaceSet].location!=NSNotFound)
{
    NSLog(@"Found");
}

if you want to restrict user use below code

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@" "])
    {
        return NO
    }
    else
    {
        return YES
    }
}

Solution 4

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

Solution 5

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;
}
Share:
32,344
A for Alpha
Author by

A for Alpha

iOS application developer and a huge steve Jobs fan. The beauty of the apple products motivated me to be a iOS apps developer and i am in love with it... :)

Updated on August 13, 2020

Comments

  • A for Alpha
    A for Alpha over 3 years

    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,if user has entered two blank spaces or complete blank spaces in the textFiled

    2 - How can i know if the textFiled is filled only with blank spaces

    edit - It is invalid to enter only white spaces(blank spaces)