-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormValidator.swift
More file actions
147 lines (117 loc) · 5.9 KB
/
FormValidator.swift
File metadata and controls
147 lines (117 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// FormValidator.swift
// FieldValidationLib
//
// Created by Mudith Chathuranga on 3/19/18.
// Copyright © 2018 Chathuranga. All rights reserved.
//
import Foundation
import UIKit
struct FormValidator {
static func checkFieldsValidity(fields: [FormField], completion: (ValidationMessage) -> ()) {
let msg = ValidationMessage()
for index in stride(from: 0, to: fields.count, by: 1) {
let trimmedData = fields[index].text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedData == "" {
msg.valid = false
msg.errorMsg = "Please enter " + fields[index].fieldName
break
} else {
let regEx = fields[index].fieldType.rawValue
let regExTest = NSPredicate(format:"SELF MATCHES %@", regEx)
msg.valid = regExTest.evaluate(with: fields[index].text)
if msg.valid == true {
msg.errorMsg = nil
} else {
msg.errorMsg = getErrorMsg(fieldType: fields[index].fieldType, fieldName: fields[index].fieldName)
break
}
}
}
completion(msg)
}
static func isValidFieldInput(input:String, fieldType: TextFieldTypes) -> Bool {
let regEx = fieldType.rawValue
let regExTest = NSPredicate(format:"SELF MATCHES %@", regEx)
return regExTest.evaluate(with: input)
}
static func getErrorMsg(fieldType: TextFieldTypes, fieldName: String) -> String {
let commonMsg = "Please enter a valid"
switch fieldType {
case TextFieldTypes.text: return "\(commonMsg) \"\(self.getBoldText(text: fieldName))\" \n\n\(self.centerText(text: "Hint:")) \n Remove extra blank spaces. "
case TextFieldTypes.name: return "\(commonMsg) \"\(self.getBoldText(text: fieldName))\" \n\n\(self.centerText(text: "Hint:")) \n Use alphabetical characters only. \n Remove extra blank spaces. "
case TextFieldTypes.email: return "\(commonMsg) \"\(self.getBoldText(text: fieldName))\""
case TextFieldTypes.digit: return "\(commonMsg) \"\(self.getBoldText(text: fieldName))\" \n\(self.centerText(text: "Hint:")) \n Phone number should include 10 digits."
case TextFieldTypes.password : return "\(commonMsg) \"\(self.getBoldText(text: fieldName))\" \n\(self.centerText(text: "Hint:")) \n Password should include 4 - 20 characters. \n Password should not contain any white spaces."
}
}
static func getBoldText(text: String) -> String {
let messageText = NSMutableAttributedString(
string: text,
attributes: [
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15.0),
//NSForegroundColorAttributeName : UIColor.blackColor()
]
)
return messageText.string
}
static func centerText(text: String) -> String {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
let messageText = NSMutableAttributedString(
string: text,
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle
//NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody),
//NSForegroundColorAttributeName : UIColor.blackColor()
]
)
return messageText.string
}
}
/////////////////////////////////// Class FormField ///////////////////////////////////
class FormField: NSObject {
var text: String!
var fieldType: TextFieldTypes!
var fieldName: String!
init(text: String, fieldType: TextFieldTypes, fieldName: String) {
self.text = text
self.fieldType = fieldType
self.fieldName = fieldName
}
}
/////////////////////////////////// Class FormField ///////////////////////////////////
/////////////////////////////////// Class ValidationMessage ///////////////////////////////////
class ValidationMessage: NSObject {
var valid: Bool! = true
var errorMsg: String? = nil
}
/////////////////////////////////// Class ValidationMessage ///////////////////////////////////
/////////////////////////////////// TextFiled Types Enum ///////////////////////////////////
enum TextFieldTypes: String {
case text = "^[^ ]+( [^ ]+)*$"
case name = "^[^ ][a-zA-Z]{0,}+( [^ ][a-zA-Z]*)*$" //^[^ ][a-zA-Z]+( [^ ][a-zA-Z]*)*$
case email = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
case digit = "^[0-9]*$"
case password = "^[a-zA-Z0-9~!@#$%^&*(){}=?><:;'\"+-_]{4,20}$" //"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,}$"
}
//Password Validators
//
//^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$ - Minimum 8 characters at least 1 Alphabet and 1 Number
//
//^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$ - Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character
//
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$ - Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number
//
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,} - Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
//
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10} - Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
//^[a-zA-Z0-9~!@#$%^&*(){}=?><:;'"+-_]{8,20}$ - Any 8characters
//Email Validators
// [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,} - email validator
//Other Validators
//^[^ ][a-zA-Z]+( [^ ][a-zA-Z]*)*$ - Name
// ^[a-zA-Z\s]+$ - All letters including white spaces
// [\S]+$ - Trailing Spaces
// ^[\S] - Starting Space
/////////////////////////////////// TextFiled Types Enum ///////////////////////////////////