├── README.md └── TextFieldValidator.swift /README.md: -------------------------------------------------------------------------------- 1 | # TextFieldValidator 2 | This is a TextFieldValidator class developed in swift. 3 | To use these validations all you need to do is just pass boolean value to one of the setter methods to enable a particular kind of validation. 4 | 5 | #Example 6 | ``` 7 | txtField.setFlagForAllowNumbersOnly(true) 8 | ``` 9 | you can also set the alert for the validation. For that, you need to set the delegate property of TextFieldValidator. 10 | 11 | ``` 12 | 01 txtField1.ParentDelegate = self 13 | 02 txtField1.allowToShowAlertView = true 14 | 03 txtField1.showAlertForNumberOnly("alert", message: "numbers only", buttonTitles: ["abc","ccc"], buttonActions: ["method1","method2"]) 15 | 16 | 04 func method1() 17 | { 18 | println("method 1 called") 19 | } 20 | func method2() 21 | { 22 | println("method2 called") 23 | } 24 | ``` 25 | 26 | 0. 01: Here we set the delegate of TextFieldValidator. 27 | 0. 02: Here we enable the alert to show. 28 | 0. 03: Here we set the "Title" and "Message" for the alertController. You can pass the button titles array and button actions array respectively to show buttons on alertController. 29 | 0. P.S. Make sure button actions are implemented in your viewController class. 30 | 0. 04: implemented methods in ViewController. 31 | -------------------------------------------------------------------------------- /TextFieldValidator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextFieldValidator.swift 3 | // TextFieldValidator 4 | // 5 | // Created by Anurag Kulkarni on 26/08/15. 6 | // Copyright (c) 2015 Anurag Kulkarni. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | let numberCharacterSet = NSCharacterSet.decimalDigitCharacterSet() 12 | 13 | let alphabetCharacterSet : NSCharacterSet = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 14 | 15 | let specialSymbolsCharacterSet : NSCharacterSet = NSCharacterSet(charactersInString: "!~`@#$%^&*-+();:=_{}[],.<>?\\/|\"\'") 16 | 17 | class TextFieldValidator: UITextField,UITextFieldDelegate { 18 | 19 | //** properties which will decide which kind of validation user wants 20 | 21 | var ParentDelegate : AnyObject? 22 | 23 | var checkForEmptyTextField : Bool = false 24 | 25 | var allowOnlyNumbers : Bool = false 26 | 27 | var allowOnlyAlphabets : Bool = false 28 | 29 | var restrictSpecialSymbolsOnly : Bool = false 30 | 31 | var checkForValidEmailAddress : Bool = false 32 | 33 | var restrictTextFieldToLimitedCharecters : Bool = false 34 | 35 | var setNumberOfCharectersToBeRestricted : Int = 0 36 | 37 | var allowToShowAlertView : Bool = false 38 | 39 | var alertControllerForNumberOnly = UIAlertController() 40 | 41 | var alertControllerForAlphabetsOnly = UIAlertController() 42 | 43 | var alertControllerForSpecialSymbols = UIAlertController() 44 | 45 | var alertControllerForInvalidEmailAddress = UIAlertController() 46 | 47 | 48 | 49 | 50 | //MARK: awakeFromNib 51 | 52 | // Setting the delegate to class 53 | 54 | override func awakeFromNib() { 55 | super.awakeFromNib() 56 | self.delegate = self 57 | } 58 | 59 | //MARK: validation methods 60 | 61 | // 01. This method will check if there are any blank textFields in class 62 | 63 | class func checkIfAllFieldsAreFilled(view:UIView) -> Bool{ 64 | 65 | let subviews : NSArray = view.subviews 66 | if(subviews.count == 0){ 67 | return false 68 | } 69 | 70 | for currentObject in subviews{ 71 | if let currentObject = currentObject as? UITextField { 72 | if(currentObject.text.isEmpty){ 73 | TextFieldValidator.shaketextField(currentObject) 74 | } 75 | } 76 | self.checkIfAllFieldsAreFilled(currentObject as! UIView) 77 | } 78 | 79 | return true 80 | } 81 | 82 | 83 | // 02. This method will check if there are any white space in the textField. 84 | 85 | class func checkForWhiteSpaceInTextField(inputString : String) -> String{ 86 | 87 | let trimmedString = inputString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 88 | 89 | return trimmedString 90 | } 91 | 92 | // 03. This method will allow only numbers in the textField. 93 | 94 | func allowOnlyNumbersInTextField(string : String)->Bool{ 95 | 96 | let numberCharacterSet = NSCharacterSet.decimalDigitCharacterSet() 97 | let inputString = string 98 | let range = inputString.rangeOfCharacterFromSet(numberCharacterSet) 99 | print(inputString) 100 | // range will be nil if no numbers are found 101 | if let test = range { 102 | 103 | return true 104 | } 105 | else { 106 | return false 107 | // do your stuff 108 | } 109 | 110 | } 111 | 112 | // 04. This method will allow only alphabets in the textField. 113 | 114 | func allowOnlyAlphabetsInTextField(string : String)->Bool{ 115 | 116 | let inputString = string 117 | let range = inputString.rangeOfCharacterFromSet(alphabetCharacterSet) 118 | print(inputString) 119 | // range will be nil if no alphabet are found 120 | if let test = range { 121 | 122 | return true 123 | } 124 | else { 125 | 126 | return false 127 | // do your stuff 128 | } 129 | 130 | 131 | } 132 | 133 | // 05. This method will restrict only special symbols in the textField. 134 | 135 | func restrictSpecialSymbols(string : String) -> Bool 136 | { 137 | let range = string.rangeOfCharacterFromSet(specialSymbolsCharacterSet.invertedSet) 138 | print(string) 139 | // range will be nil if no specialSymbol are found 140 | if let test = range { 141 | 142 | return true 143 | } 144 | else { 145 | 146 | return false 147 | // do your stuff 148 | } 149 | 150 | } 151 | 152 | 153 | 154 | //MARK: UITextFieldDelegate 155 | func textFieldShouldReturn(textField: UITextField) -> Bool { 156 | textField.resignFirstResponder() 157 | if(checkForValidEmailAddress){ 158 | let emailReg = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" 159 | let range = textField.text.rangeOfString(emailReg, options:.RegularExpressionSearch) 160 | let result = range != nil ? true : false 161 | print(result) 162 | if(result){ 163 | ParentDelegate as! UIViewController 164 | ParentDelegate!.presentViewController(alertControllerForInvalidEmailAddress, animated: true, completion: nil) 165 | return false 166 | } 167 | } 168 | return true 169 | } 170 | 171 | 172 | func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 173 | 174 | if(allowOnlyNumbers){ 175 | if(string == ""){ 176 | return true 177 | } 178 | var flag : Bool = self.allowOnlyNumbersInTextField(string) 179 | if(flag){ 180 | return true 181 | } 182 | else{ 183 | if(allowToShowAlertView){ 184 | ParentDelegate as! UIViewController 185 | ParentDelegate!.presentViewController(alertControllerForNumberOnly, animated: true, completion: nil) 186 | return false 187 | } 188 | 189 | 190 | } 191 | 192 | } 193 | else if(allowOnlyAlphabets){ 194 | if(string == ""){ 195 | return true 196 | } 197 | var flag : Bool = self.allowOnlyAlphabetsInTextField(string) 198 | if(flag){ 199 | return true 200 | } 201 | else{ 202 | if(allowToShowAlertView){ 203 | ParentDelegate as! UIViewController 204 | ParentDelegate!.presentViewController(alertControllerForAlphabetsOnly, animated: true, completion: nil) 205 | return false 206 | } 207 | 208 | } 209 | 210 | } 211 | else if(restrictSpecialSymbolsOnly){ 212 | if(string == ""){ 213 | return true 214 | } 215 | var flag : Bool = self.restrictSpecialSymbols(string) 216 | if(flag){ 217 | return true 218 | } 219 | else{ 220 | 221 | if(allowToShowAlertView){ 222 | ParentDelegate as! UIViewController 223 | ParentDelegate!.presentViewController(alertControllerForSpecialSymbols, animated: true, completion: nil) 224 | return false 225 | } 226 | 227 | } 228 | 229 | } 230 | else if(restrictTextFieldToLimitedCharecters){ 231 | 232 | let newLength = textField.text.characters.count + string.characters.count - range.length 233 | return newLength <= setNumberOfCharectersToBeRestricted 234 | } 235 | 236 | else{ 237 | return true 238 | } 239 | return false 240 | } 241 | 242 | //MARK: Setter methods 243 | func setFlagForAllowNumbersOnly(flagForNumbersOnly : Bool){ 244 | allowOnlyNumbers = flagForNumbersOnly 245 | } 246 | func setFlagForAllowAlphabetsOnly(flagForAlphabetsOnly : Bool){ 247 | allowOnlyAlphabets = flagForAlphabetsOnly 248 | } 249 | func setFlagForRestrictSpecialSymbolsOnly(RestrictSpecialSymbols : Bool){ 250 | restrictSpecialSymbolsOnly = RestrictSpecialSymbols 251 | } 252 | func setFlagForcheckForValidEmailAddressOnly(flagForValidEmailAddress : Bool){ 253 | checkForValidEmailAddress = flagForValidEmailAddress 254 | } 255 | 256 | func setFlagForLimitedNumbersOFCharecters(numberOfCharacters : Int,flagForLimitedNumbersOfCharacters : Bool){ 257 | restrictTextFieldToLimitedCharecters = flagForLimitedNumbersOfCharacters 258 | setNumberOfCharectersToBeRestricted = numberOfCharacters 259 | } 260 | 261 | 262 | 263 | //MARK: show alert methods 264 | 265 | func showAlertForNumberOnly(title: String, message: String, buttonTitles : NSArray, buttonActions: NSArray){ 266 | 267 | alertControllerForNumberOnly = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 268 | for(var i = 0 ; i < buttonActions.count; i++){ 269 | let count = i 270 | let buttonAction = UIAlertAction(title: buttonTitles[count] as! String, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 271 | if(buttonActions.count > 0){ 272 | let methodName = buttonActions[count] as! String 273 | print(methodName) 274 | NSTimer.scheduledTimerWithTimeInterval(0, target: self.ParentDelegate as! UIViewController, selector: Selector(methodName), userInfo: nil, repeats: false) 275 | } 276 | }) 277 | alertControllerForNumberOnly.addAction(buttonAction) 278 | } 279 | 280 | } 281 | 282 | func showAlertForAlphabetsOnly(title: String, message: String, buttonTitles : NSArray, buttonActions: NSArray){ 283 | 284 | alertControllerForAlphabetsOnly = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 285 | for(var i = 0 ; i < buttonActions.count; i++){ 286 | let count = i 287 | let buttonAction = UIAlertAction(title: buttonTitles[count] as! String, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 288 | 289 | if(buttonActions.count > 0){ 290 | let methodName = buttonActions[count] as! String 291 | print(methodName) 292 | NSTimer.scheduledTimerWithTimeInterval(0, target: self.ParentDelegate as! UIViewController, selector: Selector(methodName), userInfo: nil, repeats: false) 293 | } 294 | 295 | 296 | }) 297 | alertControllerForAlphabetsOnly.addAction(buttonAction) 298 | } 299 | 300 | } 301 | 302 | func showAlertForSpecialSymbolsOnly(title: String, message: String, buttonTitles : NSArray, buttonActions: NSArray){ 303 | 304 | alertControllerForSpecialSymbols = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 305 | for(var i = 0 ; i < buttonActions.count; i++){ 306 | let count = i 307 | let buttonAction = UIAlertAction(title: buttonTitles[count] as! String, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 308 | 309 | if(buttonActions.count > 0){ 310 | let methodName = buttonActions[count] as! String 311 | print(methodName) 312 | NSTimer.scheduledTimerWithTimeInterval(0, target: self.ParentDelegate as! UIViewController, selector: Selector(methodName), userInfo: nil, repeats: false) 313 | } 314 | 315 | 316 | }) 317 | alertControllerForSpecialSymbols.addAction(buttonAction) 318 | } 319 | } 320 | 321 | func showAlertForinvalidEmailAddrress(title: String, message: String, buttonTitles : NSArray, buttonActions: NSArray){ 322 | 323 | alertControllerForInvalidEmailAddress = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 324 | for(var i = 0 ; i < buttonActions.count; i++){ 325 | let count = i 326 | let buttonAction = UIAlertAction(title: buttonTitles[count] as! String, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 327 | 328 | if(buttonActions.count > 0){ 329 | let methodName = buttonActions[count] as! String 330 | print(methodName) 331 | NSTimer.scheduledTimerWithTimeInterval(0, target: self.ParentDelegate as! UIViewController, selector: Selector(methodName), userInfo: nil, repeats: false) 332 | } 333 | 334 | 335 | }) 336 | alertControllerForInvalidEmailAddress.addAction(buttonAction) 337 | } 338 | 339 | } 340 | 341 | //MARK: shake textField 342 | class func shaketextField(textfield : UITextField){ 343 | let shake:CABasicAnimation = CABasicAnimation(keyPath: "position") 344 | shake.duration = 0.1 345 | shake.repeatCount = 2 346 | shake.autoreverses = true 347 | 348 | let from_point:CGPoint = CGPointMake(textfield.center.x - 5, textfield.center.y) 349 | let from_value:NSValue = NSValue(CGPoint: from_point) 350 | 351 | let to_point:CGPoint = CGPointMake(textfield.center.x + 5, textfield.center.y) 352 | let to_value:NSValue = NSValue(CGPoint: to_point) 353 | 354 | shake.fromValue = from_value 355 | shake.toValue = to_value 356 | textfield.layer.addAnimation(shake, forKey: "position") 357 | 358 | 359 | } 360 | 361 | } 362 | --------------------------------------------------------------------------------