SwiftValidator 是一個(gè) Swift 的驗(yàn)證類庫,可以驗(yàn)證Email、電話號(hào)碼、郵政編碼等。
示例代碼:
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) }