【问题标题】:Validate a textfield (regex) in swift [closed]快速验证文本字段(正则表达式)[关闭]
【发布时间】:2015-07-12 23:19:39
【问题描述】:

我想验证一个电子邮件文本字段,以确保它接受的唯一电子邮件仅以 (.edu) 结尾(例如:example@uwf.edu 或 example.students@uwf.edu)。像 (.com)(.co)(.net) 这样的任何其他内容都是不可接受的。

【问题讨论】:

  • 这几乎可以肯定是重复的。
  • 您应该尝试一下,展示您的代码并解释您遇到的任何具体问题。 SO 不是来给你写代码的。

标签: ios iphone regex swift mobile


【解决方案1】:

试试这个(Swift 2.0):

func endsWithEdu(str : String) -> Bool {
    let regex = try! NSRegularExpression(pattern: "\\.edu$", options: [.CaseInsensitive])
    return regex.numberOfMatchesInString(str, options: [], range: NSMakeRange(0, str.characters.count)) > 0
}

print(endsWithEdu("john@university.edu")) // true
print(endsWithEdu("john@university.com")) // false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多