Giter VIP home page Giter VIP logo

validation's Introduction

Validation

A simple type to perform validations on Strings.

struct Validation {
    var required = false
    var minimumLength = 0
    var maximumLength: Int?
    var maximumValue: Double?
    var minimumValue: Double?
    var characterSet: CharacterSet?
    var format: String?
}

API

Making complete false will cause minimumLength, minimumValue and format to be ignored this is useful for partial validations, or validations where the final string is in process of been completed. For example when entering characters into an UITextField. With complete true all validations will be run.

func validateString(string: String, complete: Bool = true) -> Bool

Examples

Required (short hand for minimum value: 1)

var validation = Validation()
validation.required = true

validation.validateString("12345") // => returns true
validation.validateString("") // => returns false

Maximum or/and minimum length

var validation = Validation()
validation.minimumLength = 5
validation.maximumLength = 6

validation.validateString("1234") // => returns false
validation.validateString("12345") // => returns true
validation.validateString("1234567") // => returns false

Minimum or/and maximum value

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

validation.validateString("4") // => returns false
validation.validateString("5") // => returns true
validation.validateString("7") // => returns false

NSCharacterSet

var validation = Validation()
validation.characterSet = NSCharacterSet.decimalDigitCharacterSet()

validation.validateString("23213") // => returns true
validation.validateString("hi there") // => returns false

Format (regex)

var validation = Validation()
validation.format = "[\\w._%+-]+@[\\w.-]+\\.\\w{2,}"

validation.validateString("[email protected]") // => returns true
validation.validateString("elvnume.co") // => returns false
validation.validateString("hi there [email protected]") // => returns false

Installation

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

pod 'Validation'

License

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

Author

Elvis Nuñez, @3lvis

validation's People

Contributors

3lvis avatar bastianx6 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.