1、手機號碼驗證
移動號正則表達式:
let regex = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"
聯(lián)通號正則表達式:
let regex ="^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$"
電信號正則表達式:
let regex ="^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$"
移動、聯(lián)通想括、電信手機號通用表達式:
static func validatedPhone(phoneStr:String) -> Bool {
let phone = phoneStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^((13[0-9])|(14[5,7])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: phone)
return result
}
2、郵箱驗證
static func validatedEmail(emailStr:String) -> Bool {
let email = emailStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: email)
return result
}
3烙博、身份證號驗證
static func validatedIdentityCard(identityCardStr:String) -> Bool {
let identityCard =identityCardStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^(\\d{14}|\\d{17})(\\d|[xX])$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: identityCard)
return result
}
4瑟蜈、用戶昵稱驗證
用戶昵稱驗證(允許使用4-8個漢字)
static func validatedNickName(nickNameStr:String) -> Bool {
let nickName = nickNameStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^[\u4e00-\u9fa5]{4,8}$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: nickName)
return result
}
5烟逊、用戶名驗證
用戶名驗證(允許使用小寫字母、數(shù)字铺根、下滑線宪躯、橫杠,一共3~16個字符)
static func validatedUserName(userNameStr:String) -> Bool {
let userName = userNameStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^[a-z0-9_-]{3,16}$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: userName)
return result
}
6位迂、密碼強度驗證
密碼的強度必須是包含大小寫字母和數(shù)字的組合访雪,不能使用特殊字符,長度在8-10之間掂林。
static func validatedPassword(passwordStr:String) -> Bool {
let password = passwordStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: password)
return result
}
7臣缀、URL驗證
static func validatedURL(urlStr:String) -> Bool {
let url = urlStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: url)
return result
}
8、IP-v4地址驗證
static func validatedIPv4(ipv4Str:String) -> Bool {
let ipv4 = ipv4Str.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: ipv4)
return result
}
9泻帮、IP-v6地址驗證
static func validatedIPv6(ipv4Str:String) -> Bool {
let ipv6 = ipv6Str.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: ipv6)
return result
}
10精置、HTML標簽驗證
static func validatedHtml(htmlStr:String) -> Bool {
let html = htmlStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: html)
return result
}
11、車牌號驗證
static func validatedCarNo(carNoStr:String) -> Bool {
let carNo = carNoStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: carNo)
return result
}
12锣杂、車型驗證
static func validatedCarType(carTypeStr:String) -> Bool {
let carType = carTypeStr.trimmingCharacters(in: CharacterSet.whitespaces)
let regex = "^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$"
let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
let result = predicate.evaluate(with: carType)
return result
}