- 校验 是否只含有 “数字和字母” 或者“字母”
let regex = try? NSRegularExpression(pattern: "^(?![0-9]+$)[0-9A-Za-z]{8,16}$", options: [])
return regex?.firstMatch(in: username, options: [], range: NSRange(location: 0, length:
username.utf16.count)) != nil
}
- 校验是否 只包含“数字”或者“字母”
class func isContainsNumOrCaracters(_ str:String) -> Bool {
let regex = try? NSRegularExpression(pattern: "[0-9a-zA-Z]", options: [])
return regex?.firstMatch(in: str, options: [], range: NSRange(location: 0, length: str.utf16.count)) != nil
}
3.校验是否包含一个“大写字母”
class func isContainsOneUpLetter(_ string: String) -> Bool {
let regex = ".*[A-Z]+.*"
let test = NSPredicate(format: "SELF MATCHES %@", regex)
return test.evaluate(with: string)
}
4.校验是否包含一个“小写字母”
class func isContainsOneLowLetter(_ string: String) -> Bool {
let regex = ".*[a-z]+.*"
let test = NSPredicate(format: "SELF MATCHES %@", regex)
return test.evaluate(with: string)
}
5.校验是否包含一个“特殊字符”
class func isContainsOneSpecialCharacter(_ string: String) -> Bool {
let regex = try? NSRegularExpression(pattern: ".*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]", options: [])
let range = NSRange(location: 0, length: string.utf16.count)
let matches = regex?.matches(in: string, options: [], range: range)
return matches?.count ?? 0 > 0
}
6.校验是否连续5位相同字符
class func isContainsFiveSameStr(_ string: String) -> Bool {
let regex = try? NSRegularExpression(pattern: "([0-9a-zA-Z])\\1{4,}", options: [])
let range = NSRange(location: 0, length: string.utf16.count)
let matches = regex?.matches(in: string, options: [], range: range)
return matches?.count ?? 0 > 0
}
7.校验是否同时包含数字,字母和特殊字符
class func isFormatPassword(_ str: String) -> Bool {
let regex = try? NSRegularExpression(pattern: "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,16}", options: [])
return regex?.firstMatch(in: str, options: [], range: NSRange(location: 0, length: str.utf16.count)) != nil
}
8.校验是否大陆手机号
class func checkPhoneNum(_ string: String) -> Bool {
let phoneNumberRegEx = "^1[3-9]\\d{9}$"
let phoneTest = NSPredicate(format:"SELF MATCHES %@", phoneNumberRegEx)
return phoneTest.evaluate(with: string)
}