常用的表达式:
enum ReEnum {
case name(_: String)
case email(_: String)
case phoneNum(_: String)
case idCardNum(_: String)
case password(_: String)
case bankCard(_: String)
case chinese(_: String)
case nickname(_: String)
case decimalDigits(_: String) //小数位数
var isRight: Bool{
switch self {
case let .name(str):
// 匹配姓名包含字母、中文字符、空格和特殊字符,长度在2到50个字符之间
return NSPredicate(format: "SELF MATCHES %@", "^[a-zA-Z\\u4e00-\\u9fa5\\s\\p{P}]{2,50}$").evaluate(with: str)
case let .email(str):
// 验证邮箱
return NSPredicate(format: "SELF MATCHES %@", "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)*(\\.[a-zA-Z]{2,})$").evaluate(with: str)
case let .phoneNum(str):
//验证手机号
return NSPredicate(format: "SELF MATCHES %@", "^1\\d{10}$").evaluate(with: str)
case let .idCardNum(str):
//验证身份证号
return isTrueIDNumber(text: str)
case let .password(str):
//必须包含字母、数字和特殊字符,不能只包含这些字符的子集,且密码长度必须在8到16个字符之间
return NSPredicate(format: "SELF MATCHES %@", "^(?![a-zA-Z]+$)(?![0-9]+$)(?![~!@#$%^&*()_+|<>,.?/:;']+$)(?![0-9a-zA-Z]+$)|(?![0-9._~!@#$%^&*()_+|<>,.?/:;']+$)|(?![a-zA-Z._~!@#$%^&*()_+|<>,.?/:;']+$)[A-Za-z0-9._~!@#$%^&*()_+|<>,.?/:;'$^&*]{8,16}$").evaluate(with: str)
case let .bankCard(str):
//银行卡号验证
return checkBankCardNumber(str)
case let .chinese(str):
return NSPredicate(format: "SELF MATCHES %@", "^[\\u4e00-\\u9fa5]{0,}$").evaluate(with: str)
case let .nickname(str):
// 匹配昵称包含字母、数字、下划线和中文字符,长度在2到20个字符之间
return NSPredicate(format: "SELF MATCHES %@", "^[a-zA-Z0-9_\\u4e00-\\u9fa5]{2,20}$").evaluate(with: str)
case let .decimalDigits(str):
//限制输入两位小数
return NSPredicate(format: "SELF MATCHES %@", "^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$").evaluate(with: str)
}
}
//MARK: -验证银行卡的有效性
private func checkBankCardNumber(_ cardNumber: String?) -> Bool {
var oddSum: Int = 0 // 奇数和
var evenSum: Int = 0 // 偶数和
var allSum: Int = 0 // 总和
// 循环加和
for i in 1...(cardNumber?.count ?? 0) {
let theNumber = (cardNumber as NSString?)?.substring(with: NSRange(location: (cardNumber?.count ?? 0) - i, length: 1))
var lastNumber = Int(theNumber ?? "") ?? 0
if i % 2 == 0 {
// 偶数位
lastNumber *= 2
if lastNumber > 9 {
lastNumber -= 9
}
evenSum += lastNumber
} else {
// 奇数位
oddSum += lastNumber
}
}
allSum = oddSum + evenSum
// 是否合法
if allSum % 10 == 0 {
return true
} else {
return false
}
}
//MARK: -验证身份证号码的有效性
private func isTrueIDNumber(text:String) -> Bool{
var value = text
value = value.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
var length : Int = 0
length = value.count
if length != 15 && length != 18{
//不满足15位和18位,即身份证错误
return false
}
// 省份代码
let areasArray = ["11","12", "13","14", "15","21", "22","23", "31","32", "33","34", "35","36", "37","41", "42","43", "44","45", "46","50", "51","52", "53","54", "61","62", "63","64", "65","71", "81","82", "91"]
// 检测省份身份行政区代码
let index = value.index(value.startIndex, offsetBy: 1)
let valueStart2 = String(value[...index])
//标识省份代码是否正确
var areaFlag = false
for areaCode in areasArray {
if areaCode == valueStart2 {
areaFlag = true
break
}
}
if !areaFlag {
return false
}
var regularExpression : NSRegularExpression?
var numberofMatch : Int?
var year = 0
switch length {
case 15:
//获取年份对应的数字
let valueNSStr = value as NSString
let yearStr = valueNSStr.substring(with: NSRange.init(location: 6, length: 2)) as NSString
year = yearStr.integerValue + 1900
if year % 4 == 0 || (year % 100 == 0 && year % 4 == 0) {
//创建正则表达式 NSRegularExpressionCaseInsensitive:不区分字母大小写的模式
//测试出生日期的合法性
regularExpression = try! NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$", options: NSRegularExpression.Options.caseInsensitive)
}else{
//测试出生日期的合法性
regularExpression = try! NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$", options: NSRegularExpression.Options.caseInsensitive)
}
numberofMatch = regularExpression?.numberOfMatches(in: value, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSRange.init(location: 0, length: value.count))
if numberofMatch! > 0 {
return true
}else{
return false
}
case 18:
let valueNSStr = value as NSString
let yearStr = valueNSStr.substring(with: NSRange.init(location: 6, length: 4)) as NSString
year = yearStr.integerValue
if year % 4 == 0 || (year % 100 == 0 && year % 4 == 0) {
//测试出生日期的合法性
regularExpression = try! NSRegularExpression.init(pattern: "^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\\d{4}(((19|20)\\d{2}(0[13-9]|1[012])(0[1-9]|[12]\\d|30))|((19|20)\\d{2}(0[13578]|1[02])31)|((19|20)\\d{2}02(0[1-9]|1\\d|2[0-8]))|((19|20)([13579][26]|[2468][048]|0[048])0229))\\d{3}(\\d|X|x)?$", options: NSRegularExpression.Options.caseInsensitive)
}else{
//测试出生日期的合法性
regularExpression = try! NSRegularExpression.init(pattern: "^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\\d{4}(((19|20)\\d{2}(0[13-9]|1[012])(0[1-9]|[12]\\d|30))|((19|20)\\d{2}(0[13578]|1[02])31)|((19|20)\\d{2}02(0[1-9]|1\\d|2[0-8]))|((19|20)([13579][26]|[2468][048]|0[048])0229))\\d{3}(\\d|X|x)?$", options: NSRegularExpression.Options.caseInsensitive)
}
numberofMatch = regularExpression?.numberOfMatches(in: value, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSRange.init(location: 0, length: value.count))
if numberofMatch! > 0 {
let a = getStringByRangeIntValue(Str: valueNSStr, location: 0, length: 1) * 7
let b = getStringByRangeIntValue(Str: valueNSStr, location: 10, length: 1) * 7
let c = getStringByRangeIntValue(Str: valueNSStr, location: 1, length: 1) * 9
let d = getStringByRangeIntValue(Str: valueNSStr, location: 11, length: 1) * 9
let e = getStringByRangeIntValue(Str: valueNSStr, location: 2, length: 1) * 10
let f = getStringByRangeIntValue(Str: valueNSStr, location: 12, length: 1) * 10
let g = getStringByRangeIntValue(Str: valueNSStr, location: 3, length: 1) * 5
let h = getStringByRangeIntValue(Str: valueNSStr, location: 13, length: 1) * 5
let i = getStringByRangeIntValue(Str: valueNSStr, location: 4, length: 1) * 8
let j = getStringByRangeIntValue(Str: valueNSStr, location: 14, length: 1) * 8
let k = getStringByRangeIntValue(Str: valueNSStr, location: 5, length: 1) * 4
let l = getStringByRangeIntValue(Str: valueNSStr, location: 15, length: 1) * 4
let m = getStringByRangeIntValue(Str: valueNSStr, location: 6, length: 1) * 2
let n = getStringByRangeIntValue(Str: valueNSStr, location: 16, length: 1) * 2
let o = getStringByRangeIntValue(Str: valueNSStr, location: 7, length: 1) * 1
let p = getStringByRangeIntValue(Str: valueNSStr, location: 8, length: 1) * 6
let q = getStringByRangeIntValue(Str: valueNSStr, location: 9, length: 1) * 3
let S = a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q
let Y = S % 11
var M = "F"
let JYM = "10X98765432"
M = (JYM as NSString).substring(with: NSRange.init(location: Y, length: 1))
let lastStr = valueNSStr.substring(with: NSRange.init(location: 17, length: 1))
if lastStr == "x" {
if M == "X" {
return true
}else{
return false
}
}else{
if M == lastStr {
return true
}else{
return false
}
}
}else{
return false
}
default:
return false
}
}
private func getStringByRangeIntValue(Str : NSString,location : Int, length : Int) -> Int{
let a = Str.substring(with: NSRange(location: location, length: length))
let intValue = (a as NSString).integerValue
return intValue
}
}
仅供参考
网友评论