收藏swift正则表达式的各种验证

前端之家收集整理的这篇文章主要介绍了收藏swift正则表达式的各种验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.验证邮箱

class func validateEmail(email: String) -> Bool {
var emailString = "[A-Z0-9a-z._% -] @[A-Za-z0-9.-] \\.[A-Za-z]{2,4}"
var emailPredicate = NSPredicate(format: "SELF MATCHES %@",emailString)
return emailPredicate.evaluateWithObject(email)
}

2.验证手机号

class func validatePhonoNum(phono: String) -> Bool {
//手机号以13,15,17,18开头,八个 \d 数字字符
var phoneString = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
var phonePredicate = NSPredicate(format: "SELF MATCHES %@",phoneString)
return phonePredicate.evaluateWithObject(phono)
}

3.验证车牌号 class func validateCarNum(car: String) -> Bool { var carString = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$" var carPredicate = NSPredicate(format: "SELF MATCHES %@",carString) return carPredicate.evaluateWithObject(car) } 4.验证用户名 class func validateUserName(name: String) -> Bool { var userNameRegex = "^[A-Za-z0-9]{6,20}+$" var userNamePredicate = NSPredicate(format: "SELF MATCHES %@",userNameRegex) var peopleName = userNamePredicate.evaluateWithObject(name) return peopleName } 5.验证密码 class func validatePassword(passWord: String) -> Bool { var passWordRegex = "^[a-zA-Z0-9]{6,20}+$" var passWordPredicate = NSPredicate(format: "SELF MATCHES%@",passWordRegex) return passWordPredicate.evaluateWithObject(passWord) } 6.验证昵称 class func validateNickname(nickname: String) -> Bool { var nicknameRegex = "^[\u{4e00}-\u{9fa5}]{4,8}$" var passWordPredicate = NSPredicate(format: "SELF MATCHES%@",nicknameRegex)

原文链接:https://www.f2er.com/swift/323115.html

猜你在找的Swift相关文章