Skip to content

Commit ab2bb98

Browse files
author
Leonardo Yvens
committed
Update README.md
Added custom message example. Sanitized SSN example.
1 parent 157e979 commit ab2bb98

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

README.md

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ override func viewDidLoad() {
5353
validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])
5454

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

5859
// You can validate against other fields using ConfirmRule
5960
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])
@@ -101,31 +102,16 @@ func validationFailed(errors:[UITextField:ValidationError]) {
101102

102103
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.
103104

104-
Create a class that implements the Rule protocol
105+
Create a class that inherits from RegexRule
105106

106107
```swift
107108

108-
class SSNVRule: Rule {
109-
let REGEX = "^\\d{3}-\\d{2}-\\d{4}$"
110-
111-
init(){}
112-
113-
// allow for custom variables to be passed
114-
init(regex:String){
115-
self.REGEX = regex
116-
}
117-
118-
func validate(value: String) -> Bool {
119-
if let ssnTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
120-
if ssnTest.evaluateWithObject(value) {
121-
return true
122-
}
123-
return false
124-
}
125-
}
126-
127-
func errorMessage() -> String{
128-
return "Not a valid SSN"
109+
class SSNVRule: RegexRule {
110+
111+
static let regex = "^\\d{3}-\\d{2}-\\d{4}$"
112+
113+
convenience init(message : String = "Not a valid SSN"){
114+
self.init(regex: SSNVRule.regex, message : message)
129115
}
130116
}
131117
```

0 commit comments

Comments
 (0)