//MARK: - 验证是否是纯数字
func isNumber() -> Bool {
let pattern = "^[0-9]+$"
if NSPredicate(format: "SELF MATCHES %@", pattern).evaluate(with: self) {
return true
}
return false
}
// 验证是不是字母
func isLetter() -> Bool {
let pattern = "^^[A-Za-z0-9]+$"
if NSPredicate(format: "SELF MATCHES %@", pattern).evaluate(with: self) {
return true
}
return false
}
//MARK: - 验证是否是6位纯数字
func isNumberSix() -> Bool {
let pattern = "^\\d{6}$"
if NSPredicate(format: "SELF MATCHES %@", pattern).evaluate(with: self) {
return true
}
// let a = "aaaa"
// a.trimOptional()
return false
}
//MARK: - 验证是否是是有效提现金额
func verifyNumberTwo() -> Bool {
let pattern = "^\\d+(\\.\\d{1,2})?$"
if NSPredicate(format: "SELF MATCHES %@", pattern).evaluate(with: self) {
return true
}
return false
}
//检测银行卡
func verifyBankCard() -> Bool {
let pattern = "^([0-9]{16}|[0-9]{19}|[0-9]{17}|[0-9]{18}|[0-9]{20}|[0-9]{21})$"
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.dotMatchesLineSeparators)
if let _ = regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
return true
}
return false
}
//检测姓名
func verifyUserName() -> Bool {
let pattern = "(^[\u{4e00}-\u{9fa5}]{2,12}$)|(^[A-Za-z0-9_-]{4,12}$)"
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.dotMatchesLineSeparators)
if let _ = regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
return true
}
return false
}
//MARK: - 验证身份证
func verifyId() -> Bool {
let pattern = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.dotMatchesLineSeparators)
if let _ = regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
return true
}
return false
}
//MARK:处理手机号
func phoneNoAddAsterisk() -> String {
return (self as NSString).replacingCharacters(in: NSMakeRange(3,4), with: "****")
}
//MARK:处理手机号
func phoneNoHide() -> String {
return (self as NSString).replacingCharacters(in: NSMakeRange(2,7), with: "*******")
}
//MARK:处理银行卡号(隐藏几位)
func bankCardAddAsterisk() -> String {
if self.characters.count == 0 {
return self
}
return (self as NSString).replacingCharacters(in: NSMakeRange(4,10), with: "***********")
}
//MARK:获取字符串个数(去空格)
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
// 检测密码
func isPassword() -> Bool {
let pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$"//"^[@A-Za-z0-9!#\\$%\\^&*\\.~_]{6,20}$"
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.dotMatchesLineSeparators)
if let _ = regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
return true
}
return false
}
这些都是项目中常用到的,如有问题,请加我QQ:765950216 。大家一起学习进步。