【问题标题】:How to give validation to avoid whitespace in textField?如何进行验证以避免文本字段中的空格?
【发布时间】:2016-03-16 23:49:54
【问题描述】:

如何替换这一行

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

}

有什么建议吗?

【问题讨论】:

    标签: ios objective-c nsstring uitextfield whitespace


    【解决方案1】:

    alternateWay

    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");
    }
    

    【讨论】:

      【解决方案2】:
      NSRange range = [rawString rangeOfCharacterFromSet:whitespace];
      if (range.location != NSNotFound) {
          //whitespace Found.
      }
      

      【讨论】:

      • 请使用代码块(缩进四个空格或按{} 按钮)格式化您的代码。
      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多