Merge pull request #32 from leodasvacas/regex-messages
Custom messages for regex rules
This commit is contained in:
commit
724a05cf72
32
README.md
32
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)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue