diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1713d9f Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index b808b19..ba009ab 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ override func viewDidLoad() { validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()]) // You can pass in error labels with your rules - validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()]) + // 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)]) @@ -101,31 +102,16 @@ func validationFailed(errors:[UITextField:ValidationError]) { We will create a ```SSNRule``` class to show how to create your own Validation. A United States Social Security Number (or SSN) is a field that consists of XXX-XX-XXXX. -Create a class that implements the Rule protocol +Create a class that inherits from RegexRule ```swift -class SSNVRule: Rule { - let REGEX = "^\\d{3}-\\d{2}-\\d{4}$" - - init(){} - - // allow for custom variables to be passed - init(regex:String){ - self.REGEX = regex - } - - func validate(value: String) -> Bool { - if let ssnTest = NSPredicate(format: "SELF MATCHES %@", REGEX) { - if ssnTest.evaluateWithObject(value) { - return true - } - return false - } - } - - func errorMessage() -> String{ - return "Not a valid SSN" +class SSNVRule: RegexRule { + + static let regex = "^\\d{3}-\\d{2}-\\d{4}$" + + convenience init(message : String = "Not a valid SSN"){ + self.init(regex: SSNVRule.regex, message : message) } } ``` diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index 0223f2a..03ab470 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -9,16 +9,10 @@ import Foundation public class EmailRule: RegexRule { - - public init(){ - super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}") - } - - override public init(regex:String){ - super.init(regex: regex) - } - - override public func errorMessage() -> String { - return "Must be a valid email address" - } + + static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" + + public convenience init(message : String = "Must be a valid email address"){ + self.init(regex: EmailRule.regex, message: message) + } } \ No newline at end of file diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index baf98eb..d64af34 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -13,21 +13,14 @@ public class PasswordRule : RegexRule { // Alternative Regexes // 8 characters. One uppercase. One Lowercase. One number. - // var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).{8,}$" + // static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).{8,}$" // // no length. One uppercase. One lowercae. One number. - // var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" + // static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" + + static let regex = "^(?=.*?[A-Z]).{8,}$" - public init(){ - super.init(regex: "^(?=.*?[A-Z]).{8,}$") + public convenience init(message : String = "Must be 8 characters with 1 uppercase") { + self.init(regex: PasswordRule.regex, message : message) } - - override public init(regex: String) { - super.init(regex: regex) - } - - override public func errorMessage() -> String { - return "Must be 8 characters with 1 uppercase" - } - } \ No newline at end of file diff --git a/Validator/RegexRule.swift b/Validator/RegexRule.swift index af00238..a22123e 100644 --- a/Validator/RegexRule.swift +++ b/Validator/RegexRule.swift @@ -11,9 +11,11 @@ import Foundation public class RegexRule : Rule { private var REGEX: String = "^(?=.*?[A-Z]).{8,}$" - - public init(regex: String){ + private var message : String + + public init(regex: String, message: String = "Invalid Regular Expression"){ self.REGEX = regex + self.message = message } public func validate(value: String) -> Bool { @@ -22,6 +24,6 @@ public class RegexRule : Rule { } public func errorMessage() -> String { - return "Invalid Regular Expression" + return message } } diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index 0df941f..360ccce 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -10,16 +10,7 @@ import Foundation public class ZipCodeRule: RegexRule { - public init(){ - super.init(regex: "\\d{5}") + public convenience init(message : String = "Enter a valid 5 digit zipcode"){ + self.init(regex: "\\d{5}", message : message) } - - override public init(regex: String) { - super.init(regex: regex) - } - - public override func errorMessage() -> String { - return "Enter a valid 5 digit zipcode" - } - } \ No newline at end of file