How to know if a UITextField in iOS has blank spaces

前端 未结 9 829
一整个雨季
一整个雨季 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: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;
    }
    

提交回复
热议问题