如何进行验证以避免文本字段中出现空格?

How to give validation to avoid whitespace in textField?

如何替换这一行

[txtEmail.textstringByTrimmingCharactersInSet[NSCharacterSetwhitespaceAndNewlineCharacterSet]].length == 0`

通过任何函数。

if ([txtEmail.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0 && 
   [txtPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0)
{

}
else if ([txtEmail.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0)
{

}

有什么建议吗?

替代方式

txtEmail.text = [txtEmail.text stringByReplacingOccurrencesOfString: @" " withString: @""];

if (txtEmail.text.length == 0)
{
// show alert
}
else
{
 // do your stuff here
}

选项-2

if([txtEmail.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location!=NSNotFound)
{
NSLog(@"white space Found");
}
NSRange range = [rawString rangeOfCharacterFromSet:whitespace];
if (range.location != NSNotFound) {
    //whitespace Found.
}