欢迎加入QQ讨论群258996829

Swift 验证类库 SwiftValidator

发布时间:2016-09-06 17:02  回复:0  查看:5346  感兴趣:55  赞:1   最后回复:2016-09-06 17:02  

SwiftValidator 是一个 Swift 的验证类库,可以验证Email、电话号码、邮政编码等。

Swift 验证类库 SwiftValidator

示例代码:

Initialize the Validator by setting a delegate to a View Controller or other object.

// ViewController.swift
let validator = Validator()
Register the fields that you want to validate
override func viewDidLoad() {
    super.viewDidLoad()

    // Validation Rules are evaluated from left to right.
    validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])

    // You can pass in error labels with your rules
    // You can pass in custom error messages to regex rules (such as ZipCodeRule and EmailRule)
    validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule(message: "Invalid email")])

    // You can validate against other fields using ConfirmRule
    validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])

    // You can now pass in regex and length parameters through overloaded contructors
    validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
    validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])

    // You can unregister a text field if you no longer want to validate it
    validator.unregisterField(fullNameTextField)
}

相关开源代码

您还未登录,请先登录

热门帖子

最新帖子