如何进行验证以避免textField中的空格?

时间:2015-12-11 06:15:18

标签: ios objective-c nsstring uitextfield whitespace

如何替换此行

[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)
{

}

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

alternateWay




  txtEmail.text = [txtEmail.text stringByReplacingOccurrencesOfString:@“”withString:@“”];& #xA;
 if(txtEmail.text.length == 0)
 {
 // show alert
}
 else
 {
 //在这里做你的东西
}
  




选项-2

&#xA ;

  if([txtEmail.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]。location!= NSNotFound)
 {
 NSLog(@“white space found”);& #xA;}
  



答案 1 :(得分:0)

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