【问题标题】:How To Restrict A Username Textfield [duplicate]如何限制用户名文本字段 [重复]
【发布时间】:2018-04-17 21:42:35
【问题描述】:

在我的项目中,用户可以输入他们想要的用户名。我想限制这个字段,他们只能使用字母、数字和下划线(如 Instagram)。我已经在互联网上搜索了几个小时,但什么都想不出来。我怎样才能做到这一点?提前谢谢!

【问题讨论】:

  • 看看this问题,它完全描述了它:)

标签: swift uitextfield


【解决方案1】:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
      let s = NSString(string: textField.text ?? "").replacingCharacters(in: range, with: string)
      guard !s.isEmpty else { return true }
      let numberFormatter = NumberFormatter()
      numberFormatter.numberStyle = .none
      return numberFormatter.number(from: s)?.intValue != nil
 }

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多