diff --git a/.DS_Store b/.DS_Store index f47eb24..401679a 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/ValidationsDemo.xcodeproj/project.xcworkspace/xcuserdata/manraajnijjar.xcuserdatad/UserInterfaceState.xcuserstate b/ValidationsDemo.xcodeproj/project.xcworkspace/xcuserdata/manraajnijjar.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d866029 Binary files /dev/null and b/ValidationsDemo.xcodeproj/project.xcworkspace/xcuserdata/manraajnijjar.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ValidationsDemo.xcodeproj/xcuserdata/manraajnijjar.xcuserdatad/xcschemes/xcschememanagement.plist b/ValidationsDemo.xcodeproj/xcuserdata/manraajnijjar.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..58a7c63 --- /dev/null +++ b/ValidationsDemo.xcodeproj/xcuserdata/manraajnijjar.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + ValidationsDemo.xcscheme + + orderHint + 0 + + + + diff --git a/ValidationsDemo/Base.lproj/Main.storyboard b/ValidationsDemo/Base.lproj/Main.storyboard index 568378d..75403d2 100644 --- a/ValidationsDemo/Base.lproj/Main.storyboard +++ b/ValidationsDemo/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -84,8 +84,17 @@ + + + + + + + + + + + + + + + + + + + + - + + + + + @@ -131,6 +155,8 @@ + + diff --git a/ValidationsDemo/TextFieldValidation/TextFieldValidation.swift b/ValidationsDemo/TextFieldValidation/TextFieldValidation.swift index b8da3aa..ebfa84c 100644 --- a/ValidationsDemo/TextFieldValidation/TextFieldValidation.swift +++ b/ValidationsDemo/TextFieldValidation/TextFieldValidation.swift @@ -72,7 +72,13 @@ internal extension UITextField { case let .range(min, max, message): try rangeValidation(min: min, max: max, message: message) + case let .filterMessageBase(message): + try wordFilteredValidation(message: message) + + case let .filterMessageExhaustive(message): + try wordFullFilterValidation(message: message) } + } } catch { @@ -166,6 +172,37 @@ internal extension UITextField { } } + private func wordFilteredValidation(message: String) throws { + let wordsInText = text?.split(separator:" "); + for word in wordsInText! { + if ValidationPreferences.wordsForFilter.contains(String(word)){ + throw generateException(message); + } + } + } + + private func wordFullFilterValidation(message: String) throws { + let regexPatternForSpecialChars = "\\W+" + let regexFilter = try! NSRegularExpression(pattern: regexPatternForSpecialChars, options: NSRegularExpression.Options.caseInsensitive) + let textRange = NSMakeRange(0, (text?.characters.count)!) + var textForFilter = regexFilter.stringByReplacingMatches(in: text!, options: [], range: textRange, withTemplate: "") + textForFilter = textForFilter.lowercased() + var wordsForFilterDictionary = [String : Bool]() + for word in ValidationPreferences.wordsForFilter { + wordsForFilterDictionary[String(word)] = true + } + for firstCharIndex in 0 ... textForFilter.characters.count { + for endCharIndex in firstCharIndex ... textForFilter.characters.count { + let startIndexForSubstring = textForFilter.index(textForFilter.startIndex, offsetBy: firstCharIndex) + let endIndexForSubstring = textForFilter.index(textForFilter.startIndex, offsetBy: endCharIndex) + let rangeForSubstring = startIndexForSubstring.. Error { return NSError(domain: ValidationPreferences.domain, code: ValidationPreferences.errorCode, userInfo: [NSLocalizedDescriptionKey: message, "textField":self]) as Error diff --git a/ValidationsDemo/TextFieldValidation/ValidationPreferences.swift b/ValidationsDemo/TextFieldValidation/ValidationPreferences.swift index aa8df24..ef8c940 100644 --- a/ValidationsDemo/TextFieldValidation/ValidationPreferences.swift +++ b/ValidationsDemo/TextFieldValidation/ValidationPreferences.swift @@ -17,6 +17,8 @@ enum Validation { case characterRange(min:Int, max:Int, message: String) case alphaNumeric(message: String) // Only allowed A-Z lower or upper case or blank space or numeric values. case range(min:Int, max:Int, message: String) // Range only apply on numeric values + case filterMessageBase(message: String) + case filterMessageExhaustive(message: String) } struct ValidationPreferences { @@ -27,4 +29,5 @@ struct ValidationPreferences { static let passwordRegEx = "(.{6,12})" static let domain = "VALIDATIONFAILED" static let errorCode = 501 + static let wordsForFilter = [String]() } diff --git a/ValidationsDemo/ViewController.swift b/ValidationsDemo/ViewController.swift index 1b99d3b..87d67a0 100644 --- a/ValidationsDemo/ViewController.swift +++ b/ValidationsDemo/ViewController.swift @@ -17,7 +17,9 @@ class ViewController: UIViewController { @IBOutlet weak var passwordTextField: UITextField! @IBOutlet weak var ageTextField: UITextField! @IBOutlet weak var alphaNumericTextField: UITextField! - + @IBOutlet weak var wordFilterTextField: UITextField! + @IBOutlet weak var wordFilterThoroughTextField: UITextField! + override func viewDidLoad() { super.viewDidLoad() @@ -49,6 +51,8 @@ extension ViewController { requiredTextField.validations = [Validation.required(message: "Text field is required")] ageTextField.validations = [Validation.range(min: 18, max: 70, message: "Invalid age value")] alphaNumericTextField.validations = [Validation.alphaNumeric(message: "Invalid alphanumeric textfield value")] + wordFilterTextField.validations = [Validation.filterMessageBase(message: "Sorry these words are not allowed")] + wordFilterThoroughTextField.validations = [Validation.filterMessageExhaustive(message: "Sorry these words are not allowed")] } fileprivate func checkValidation() -> Bool{ @@ -60,6 +64,8 @@ extension ViewController { try requiredTextField.validate() try ageTextField.validate() try alphaNumericTextField.validate() + try wordFilterTextField.validate() + try wordFilterThoroughTextField.validate() return true } catch{