swift 郵箱、密碼、手機號、身份證驗證正則
/驗證郵箱
class func validateEmail(email: String) -> Bool {
if email.count == 0 {
return false
}
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
return emailTest.evaluate(with: email)
}
//驗證手機號
class func isPhoneNumber(phoneNumber:String) -> Bool {
if phoneNumber.count == 0 {
return false
}
let mobile = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$"
let regexMobile = NSPredicate(format: "SELF MATCHES %@",mobile)
if regexMobile.evaluate(with: phoneNumber) == true {
return true
}else
{
return false
}
}
//密碼正則 6-8位字母和數字組合
class func isPasswordRuler(password:String) -> Bool {
let passwordRule = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,8}$"
let regexPassword = NSPredicate(format: "SELF MATCHES %@",passwordRule)
if regexPassword.evaluate(with: password) == true {
return true
}else
{
return false
}
}
//驗證身份證號
class 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: 2)
let valueStart2 = value.substring(to: 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
}
}
相關文章
- 編寫正則:匹配郵箱 手機號 密碼以及身份證號密碼
- vue.js帳號,密碼,郵箱和移動手機號碼正則驗證Vue.js密碼
- js正規表示式驗證手機,郵箱,身份證JS
- jQuery驗證手機號郵箱身份證的正規表示式(含港澳臺)jQuery
- 手機號碼驗證方法(正則驗證)
- js正則驗證身份證號JS
- 正則校驗手機號和郵箱
- Javascript使用正則驗證身份證號(簡單)JavaScript
- 手機、電話號碼、郵箱、域名、身份證號的測試用例
- 驗證手機、郵箱、漢字、身份證、URL、IP地址等java程式碼工具類Java
- js中使用正則驗證手機號JS
- PHP 驗證身份證號碼PHP
- js表情正則 手機正則 郵箱正則JS
- 賬戶、密碼格式···正則驗證密碼
- jQuery如何用正規表示式驗證手機號、身份證號、中文名稱jQuery
- 正則匹配案例(驗證是否是一個有效數字、驗證手機號、驗證郵箱、匹配一個漢字範圍、匹配一個漢字姓名)...
- 手機號碼驗證
- 郵箱地址正規表示式驗證
- JS驗證18位身份證號的正確性JS
- javascript身份證號碼校驗JavaScript
- 正規表示式驗證郵箱及其解析
- js驗證手機號碼JS
- 郵箱格式驗證
- js驗證郵箱JS
- 身份證號碼的正規表示式及驗證詳解(JavaScript,Regex)JavaScript
- C++身份證號驗證C++
- 匹配身份證號碼正規表示式
- 精準驗證手機號碼格式正規表示式
- 直播app原始碼,驗證方式選擇郵箱驗證時,自動給輸入好的郵箱傳送驗證碼APP原始碼
- Java實現郵箱驗證碼功能Java
- JavaScript郵箱格式驗證JavaScript
- 中國身份證號驗證庫
- JQuery表單驗證(包括:使用者名稱,手機號,密碼,確認密碼,驗證碼60s)jQuery密碼
- java正規表示式替換手機號,身份證號處理 星號Java
- ng 姓名,手機,身份證號碼 脫敏處理
- 手機號碼簡訊驗證註冊
- 直播系統原始碼,選擇驗證方式時選擇郵箱驗證原始碼
- 強密碼驗證正規表示式密碼