基础应用

Swift - 手机号.邮箱等其他正则表达式

2018-09-23  本文已影响26人  朴子hp

 1.验证手机号

    static func validatePhonoNum(phono:String) ->Bool{

        //手机号以13,15,17,18开头,八个 \d 数字字符

        let phoneString = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"

        let phonePredicate =NSPredicate(format:"SELF MATCHES %@", phoneString)

        return phonePredicate.evaluate(with: phono)

    }

 2.验证邮箱

    static func validateEmail(email:String) ->Bool{

        let emailString = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"

        let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailString)

        return emailPredicate.evaluate(with: email)

    }

3.是否是纯数字

static func validateNumber(number:String) ->Bool{

        let numberString ="^[0-9]+$"

        let numberPredicate =NSPredicate(format:"SELF MATCHES %@", numberString)

        return numberPredicate.evaluate(with: number)

    }

4.是否是纯字母

static func validateLetter(letter:String) -> Bool {

        let letterString = "^[A-Za-z]+$"

        let letterPredicate = NSPredicate(format:"SELF MATCHES %@", letterString)

        return letterPredicate.evaluate(with: letter)

    }

5.密码验证-6到10位长的字符串,只能包含大小写字母和数字!!!

static func validatePassword(password: String) -> Bool {

        let passwordStr ="^[A-Za-z0-9]{6,10}+$"

        let passwordPredicate = NSPredicate(format:"SELF MATCHES %@", passwordStr)

        return passwordPredicate.evaluate(with: password)

    }

以后会不定期持续更新...

上一篇 下一篇

猜你喜欢

热点阅读