代码如下:
import UIKit
class MMTest: NSObject {
//MARK:- 正则匹配手机号
class func checkMobile(_ mobileNumbel:NSString) ->Bool
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189,181(增加)
*/
let MOBIL = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
* 中国移动:China Mobile
* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
/**
* 中国联通:China Unicom
* 130,131,132,152,155,156,185,186
*/
let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
* 中国电信:China Telecom
* 133,1349,153,180,189,181(增加)
*/
let CT = "^1((33|53|8[019])[0-9]|349)\\d{7}$";
let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBIL)
let regextestcm = NSPredicate(format: "SELF MATCHES %@", CM)
let regextestcu = NSPredicate(format: "SELF MATCHES %@", CU)
let regextestct = NSPredicate(format: "SELF MATCHES %@", CT)
if regextestmobile.evaluate(with: mobileNumbel)||regextestcm.evaluate(with: mobileNumbel)||regextestcu.evaluate(with: mobileNumbel)||regextestct.evaluate(with: mobileNumbel) {
return true
}
return false
}
//MARK:- 正则匹配用户身份证号15或18位
class func checkUserIdCard(_ idCard:NSString) ->Bool {
let pattern = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
let isMatch:Bool = pred.evaluate(with: idCard)
return isMatch;
}
//MARK:- 正则匹配用户密码6-18位数字和字母组合
class func checkPassword(_ password:NSString) ->Bool {
let pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"
let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
let isMatch:Bool = pred.evaluate(with: password)
return isMatch;
}
//MARK:- 正则匹配URL
class func checkURL(_ url:NSString) ->Bool {
let pattern = "^[0-9A-Za-z]{1,50}"
let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
let isMatch:Bool = pred.evaluate(with: url)
return isMatch;
}
//MARK:- 正则匹配用户姓名,20位的中文或英文
class func checkUserName(_ userName:NSString) ->Bool {
let pattern = "^[a-zA-Z\\u4E00-\\u9FA5]{1,20}"
let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
let isMatch:Bool = pred.evaluate(with: userName)
return isMatch;
}
//MARK:- 正则匹配用户email
class func checkEmail(_ email:NSString) ->Bool {
let pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
let isMatch:Bool = pred.evaluate(with: email)
return isMatch;
}
}
网友评论