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 50b83d4..98adc08 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -9,6 +9,7 @@ import Foundation public class EmailRule: RegexRule { + static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index 772f50a..fdaea8e 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -17,10 +17,18 @@ public class PasswordRule : RegexRule { // // no length. One uppercase. One lowercae. One number. // static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" +<<<<<<< HEAD static let regex = "^(?=.*?[A-Z]).{8,}$" public convenience init(message : String = "Must be 8 characters with 1 uppercase") { self.init(regex: PasswordRule.regex, message : message) +======= + + static let regex = "^(?=.*?[A-Z]).{8,}$" + + public convenience init(message : String = "Must be 8 characters with 1 uppercase") { + self.init(regex: PasswordRule.regex, message : message) +>>>>>>> upstream/master } } \ No newline at end of file diff --git a/Validator/RegexRule.swift b/Validator/RegexRule.swift index 42f6fb5..64ac49b 100644 --- a/Validator/RegexRule.swift +++ b/Validator/RegexRule.swift @@ -12,10 +12,17 @@ public class RegexRule : Rule { private var REGEX: String = "^(?=.*?[A-Z]).{8,}$" private var message : String +<<<<<<< HEAD public init(regex: String, message: String = "Invalid Regular Expression"){ self.REGEX = regex self.message = message +======= + + public init(regex: String, message: String = "Invalid Regular Expression"){ + self.REGEX = regex + self.message = message +>>>>>>> upstream/master } public func validate(value: String) -> Bool { diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index 52a5f7a..daf2c6a 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -10,7 +10,12 @@ import Foundation public class ZipCodeRule: RegexRule { +<<<<<<< HEAD public convenience init(message : String = "Enter a valid 5 digit zipcode"){ self.init(regex: "\\d{5}", message : message) +======= + public convenience init(message : String = "Enter a valid 5 digit zipcode"){ + self.init(regex: "\\d{5}", message : message) +>>>>>>> upstream/master } } \ No newline at end of file