SwiftValidator 是一个 Swift 的验证类库,可以验证Email、电话号码、邮政编码等。
示例代码:
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) }