Giter Site home page Giter Site logo

formtextfield's Introduction

FormTextField

This a UITextField subclass that supports styling for checking for valid and invalid inputs, and formatters so you can easily format credit card numbers, phone numbers and more. It supports input validators so you can limit the contents of a UITextField using maximum length, maximum value or even regex (perfect for validating emails).

Table of Contents

Styling

FormTextField also supports styling using UIAppearance protocol. The example shown above uses this for styling.

Input Validators

The InputValidator object allows you to validate a value by setting some rules, out of the box InputValidator allows you to validate:

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

For example if you have a FormTextField where you only want to allow values between 5 and 6 you can do this:

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

formTextField.inputValidator = InputValidator(validation: validation)

Typing 4 => Invalid
Typing 5 => Valid
Typing 6 => Valid
Typing 7 => Invalid

FormTextField includes 3 built-in input validators:

  • CardExpirationDate: Validates MM/YY, where MM is month and YY is year. MM shouldn't be more than 12 and year can be pretty much any number above the current year (this to ensure that the card is not expired).

  • Decimal: Validates that the value is a number allowing commas and dots for decimal separation.

  • Required: A convenience input validator for minimum length 1.

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.count > 0 {
                let letterCharacterSet = NSCharacterSet.letterCharacterSet()
                let stringCharacterSet = NSCharacterSet(charactersInString: composedString)
                valid = letterCharacterSet.isSupersetOfSet(stringCharacterSet)
            }
        }

        return valid
    }
}

formTextField.inputValidator = LetterInputValidator()
Typing A => Valid
Typing 2 => Invalid
Typing AA => Valid
Typing A7 => Invalid

Formatters

Formatter objects are objects that convert your text to a specific formated implemented using the Formattable protocol. Out of the box FormTextField includes two Formatters:

CardExpirationFormatter: Formats a number so it follows the MM/YY convention where MM is month and YY is year.

CardNumberFormatter: Formats a number so it adds a separation after every 4th character, for example it will format 1234567812345678 as 1234 5678 1234 5678.

Making your own Formatters

Making a custom Formatter for FormTextField should be as simple as making a class that conforms to the Formattable protocol, meaning implementing the following method.

func formatString(_ string: String, reverse: Bool) -> String

Installation

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

pod 'FormTextField'

License

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

Author

Elvis Nuñez, @3lvis

formtextfield's People

Contributors

3lvis avatar dekablade01 avatar gitter-badger avatar jaredhalpern avatar jdieman avatar lightman avatar lugearma avatar marijnschilling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

formtextfield's Issues

unable to delete card expiry date from backspace key.

Hello Dev

I am using your pod in my project with versions of 3.0.0. There is an issue like user unable to delete card expiry date from backspace key. but in your demo project its working fine.

I think you need to push your latest pod on the cocoapods with updated versions for these classes.

  1. CardExpirationDateFormatter
  2. CardExpirationDateInputValidator

So that I can update the latest pods in my project pod file. :)

You are doing nice work. Keep it up.

Thanks

Chinese character input issue

I want to limit the length of characters in the input box.
My main code is as follows. However, when inputting Chinese characters, only the first character can be input into Chinese. There is no way to input Chinese later. I tried to record a video. Can you tell me what the problem is?
System Ver. iOS 13

var validation = Validation()
validation.minimumLength = minLength
validation.maximumLength = maxLength
titleTextField.inputValidator = InputValidator(validation: validation)

QQ20200724-191025.mp4.zip

Unable to delete card expiry date from backspace key.

Hi,

unable to delete card expiry date from backspace key.

I am using your pod in my project with versions of 3.0.0. There is an issue like the user unable to delete the card expiry date from the backspace key.

Swift Package Manager

Hello there and great work !

Any chance to see SPM support anytime soon ? It would be a great addition.

It's my last dep still on CocoaPods in my current project 🙂

It crashes if I type any emoji

Here is the log cat that Xcode is showing up. Could you give any advices?

let index = composedString.characters.index(composedString.startIndex, offsetBy: range.location)

American Express cards are invalid

A typical credit card number (i.e. Mastercard) looks like this:
5555 5555 5555 4444
and an American Express card looks like
3782 822463 10005

Using the Default CardNumberFormatter, American Express cards are invalid. Can we get some sort of formatter that can be used with both?

Add UI tests

Could test:

  • Color state when switching between: enabled, disabled, valid, invalid, active and inactive.
  • Cursor position when inserting text: at the end, at the middle, at the beginning and just before a jump when formatting.
  • Invalid state of all fields when pressing "Pay" button and valid state after filling invalid fields or just jumping between them.

Broken link

The link to the formatters documentation gives a 404

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.