Swift - 正则验证

[来自这里!]

(http://blog.csdn.net/h643342713/article/details/52424523#)
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)

你可能感兴趣的:(Swift - 正则验证)