Skip to content

Commit

Permalink
Updating for swift 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpotts18 committed Mar 24, 2015
1 parent ae514e2 commit ff25465
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 107 deletions.
Binary file not shown.
118 changes: 59 additions & 59 deletions Validator/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

26 changes: 10 additions & 16 deletions Validator/EmailRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,25 @@ import Foundation

class EmailRule: Rule {

let REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
var REGEX : String

init(){}

init(regex:String){
self.REGEX = regex
init(){
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
}

var message:String {
return "Must be a valid email address"
init(regex:String){
REGEX = regex
}

func validate(value:String) -> Bool {

if let emailTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
if emailTest.evaluateWithObject(value) {
return true
} else {
return false
}
func validate(value: String) -> Bool {
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
if test.evaluateWithObject(value) {
return true
}
return false
}

func errorMessage() -> String {
return self.message
return "Must be a valid email address"
}
}
16 changes: 9 additions & 7 deletions Validator/MinLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ import Foundation

class MinLengthRule : Rule {

let DEFAULT_MIN_LENGTH:Int = 3
private let DEFAULT_MIN_LENGTH: Int

init(){}
init(){
DEFAULT_MIN_LENGTH = 3
}

init(length:Int){
self.DEFAULT_MIN_LENGTH = length
}

func errorMessage() -> String {
return "Must be at least \(DEFAULT_MIN_LENGTH) characters long"
}

func validate(value: String) -> Bool {
if countElements(value) <= DEFAULT_MIN_LENGTH {
if count(value) <= DEFAULT_MIN_LENGTH {
return false
}
return true
}

func errorMessage() -> String {
return "Must be at least \(DEFAULT_MIN_LENGTH) characters long"
}
}
20 changes: 8 additions & 12 deletions Validator/PasswordRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,25 @@ class PasswordRule : Rule {

// 8 characters. one uppercase

var REGEX = "^(?=.*?[A-Z]).{8,}$"
private let REGEX: String

init(){}
init(){
self.REGEX = "^(?=.*?[A-Z]).{8,}$"
}

init(regex:String){
self.REGEX = regex
}

var message:String {
return "Must be 8 characters with 1 uppercase"
}

func validate(value: String) -> Bool {
if let passwordTes = NSPredicate(format: "SELF MATCHES %@", REGEX) {
if passwordTes.evaluateWithObject(value) {
return true
}
return false
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
if test.evaluateWithObject(value) {
return true
}
return false
}

func errorMessage() -> String {
return self.message
return "Must be 8 characters with 1 uppercase"
}
}
5 changes: 5 additions & 0 deletions Validator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
override func viewDidLoad() {
super.viewDidLoad()

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "hideKeyboard"))

validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()])
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
Expand Down Expand Up @@ -96,6 +97,10 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
error.errorLabel?.hidden = true
}
}

func hideKeyboard(){
self.view.endEditing(true)
}

}

21 changes: 8 additions & 13 deletions Validator/ZipCodeRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,25 @@
import Foundation

class ZipCodeRule: Rule {
let REGEX = "\\d{5}"
private let REGEX: String


init(){}
init(){
self.REGEX = "\\d{5}"
}
init(regex:String){
self.REGEX = regex
}

var message: String {
return "Enter a valid 5 digit zipcode"
}

func validate(value: String) -> Bool {
if let zipTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
if zipTest.evaluateWithObject(value) {
return true
}
return false
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
if test.evaluateWithObject(value) {
return true
}
return false
}

func errorMessage() -> String {
return message
return "Enter a valid 5 digit zipcode"
}

}

0 comments on commit ff25465

Please sign in to comment.