Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

ios - Email & Phone Validation in Swift

i am using the following code for phone number validation. But i am getting the following error. I cant able to proceed further. Help us to take it forward.

class PhoneNumberValidation: Validation {
    let PHONE_REGEX = "^\d{3}-\d{3}-\d{4}$"

    func validate(value: String) -> (Bool, ValidationErrorType) {
        if let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX) {
            if phoneTest.evaluateWithObject(value) {
                return (true, .NoError)
            }
            return (false, .PhoneNumber)
        }
        return (false, .PhoneNumber)
    }
}

Error : swift:15:28: Bound value in a conditional binding must be of Optional type

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes your Error is below in XCode 6.1

Xcode error screenshot

This error is occurs because of if conditional have to Bool return type, but in Your if condition Return type is NSPredicate so that you got error swift: Bound value in a conditional binding must be of Optional type you can solve as below.

    var phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
         if phoneTest.evaluateWithObject(value) {
                  return (true, .NoError)
             }
                 return (false, .PhoneNumber)
            }

Email-Validations in Swift.

    func isValidEmail(testStr:String) -> Bool {
            print("validate emilId: (testStr)")
            let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\t| )*\r\n)?(?:\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\[|\])|(?:\\(?:\t|[ -~]))))+(?: )*)|(?: )+)"))(?:@)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\])))(?:(?:(?:(?: )*(?:(?:(?:\t| )*\r\n)?(?:\t| )+))+(?: )*)|(?: )+)?$"
            let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
            let result = emailTest.evaluateWithObject(testStr)
            return result
        } 

Use of Email-validation:

    if isValidEmail("kirit@gmail.com"){
            print("Validate EmailID")
        }
        else{
            print("invalide EmailID")
        }

Phone-Number Validation in Swift

    func validate(value: String) -> Bool {
            let PHONE_REGEX = "^\d{3}-\d{3}-\d{4}$"
            let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
            let result = phoneTest.evaluate(with: value)
            return result
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...