Latest 1.0.3
Homepage https://github.com/3lvis/InputValidator
License MIT
Platforms ios 9.0, tvos 9.0, requires ARC
Dependencies Validation
Frameworks Foundation
Authors

Introduction

InputValidator is the easiest way to validate a value, things that InputValidator lets you validate:

  • Required (non-empty)
  • Maximum length
  • Minimum length
  • Maximum value
  • Minimum value
  • Valid characters
  • Format (regex)

For example if you want to validate that a value is between 5 and 6 you can do this:

let validation = Validation()
validation.minimumValue = 5
validation.maximumValue = 6

var result: Bool
let validator = InputValidator(validation: validation)
result = validator.validateString("4") // false
result = validator.validateString("5") // true
result = validator.validateString("6") // true
result = validator.validateString("7") // false

Find more information about the basic validations on the Validation repository.

It also helps you verify if a string should be inserted into another string, useful when validating inputs on a UITextField. For example validating the expiration date of a card, where the format is MM/YY, month is between 1-12 and YY is equal or later than the current year.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (!string || [string isEqualToString:@"n"]) return YES;

    CardExpirationDateInputValidator *inputValidator = [CardExpirationDateInputValidator new];
    return [inputValidator validateReplacementString:string withText:self.text withRange:range];
}

Included built-in input validators

  • CardExpirationDate
  • Decimal

Making your own input validator

InputValidator includes the InputValidatable protocol. Any class that conforms to this protocol can be considered an input validator. For example making an InputValidator that only allows letters could be as simple as this.

public struct LetterInputValidator: InputValidatable {
    public var validation: Validation?

    public init(validation: Validation? = nil) {
        self.validation = validation
    }

    public func validateReplacementString(replacementString: String?, fullString: String?, inRange range: NSRange?) -> Bool {
        var valid = true
        if let validation = self.validation {
            let evaluatedString = self.composedString(replacementString, fullString: fullString, inRange: range)
            valid = validation.validateString(evaluatedString, complete: false)
        }

        if valid {
            let composedString = self.composedString(replacementString, fullString: fullString, inRange: range)
            if composedString.characters.count > 0 {
                let letterCharacterSet = NSCharacterSet.letterCharacterSet()
                let stringCharacterSet = NSCharacterSet(charactersInString: composedString)
                valid = letterCharacterSet.isSupersetOfSet(stringCharacterSet)
            }
        }

        return valid
    }
}

let validator = LetterInputValidator()
result = validator.validateString("A") // true
result = validator.validateString("2") // false
result = validator.validateString("AA") // true
result = validator.validateString("A7-w") // false

Installation

InputValidator is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'InputValidator'

License

InputValidator is available under the MIT license. See the LICENSE file for more info.

Author

Elvis Nuñez, @3lvis

Latest podspec

{
    "name": "InputValidator",
    "summary": "Input Validators allow you to verify incoming input",
    "version": "1.0.3",
    "homepage": "https://github.com/3lvis/InputValidator",
    "license": "MIT",
    "authors": {
        "Elvis Nuu00f1ez": "[email protected]"
    },
    "source": {
        "git": "https://github.com/3lvis/InputValidator.git",
        "tag": "1.0.3"
    },
    "social_media_url": "https://twitter.com/3lvis",
    "platforms": {
        "ios": "9.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "dependencies": {
        "Validation": [
            "~> 1.0.2"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This