• Stars
    star
    186
  • Rank 207,316 (Top 5 %)
  • Language
    Swift
  • License
    Other
  • Created about 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UITextField with support for formatters and input validators

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

Native Demo

Custom Demo

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

More Repositories

1

Sync

JSON to Core Data and back. Swift Core Data Sync.
Swift
2,552
star
2

Form

The most flexible and powerful way to build a form on iOS
Objective-C
1,644
star
3

Networking

Swift HTTP Networking with stubbing and caching support
Swift
1,360
star
4

Viewer

Image viewer (or Lightbox) with support for local and remote videos and images
Swift
536
star
5

DATAStack

100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer
Swift
215
star
6

SectionScrubber

A component to quickly scroll between collection view sections
Swift
188
star
7

DATASource

Core Data's NSFetchedResultsController wrapper for UITableView and UICollectionView
Swift
106
star
8

CardStack

DEPRECATED
Swift
37
star
9

Hex

Hex support for UIColor, all in Swift
Swift
24
star
10

app-template

Swift storyboard-less well structured iOS app template
Swift
17
star
11

DateParser

Simple ISO 8601 and Unix timestamp Swift date parser
Swift
15
star
12

DATAFilter

Filter inserts, updates and deletions from your JSON response
Swift
15
star
13

NetworkActivityIndicator

A library that helps managing the network activity indicator state
Swift
11
star
14

PaginatedScrollView

Paginated UIScrollView, a simple UIPageViewController alternative written in Swift.
Swift
10
star
15

AppNetDemo

Sync's Swift App.Net Demo [Deprecated]
Swift
9
star
16

MD5

[DEPRECATED]
Swift
6
star
17

DATAFastQuery

The fastest way to query Core Data
Swift
5
star
18

ControllerContainer

View Controller Containment for humans
Swift
5
star
19

iOS-playbook

Guidelines and best practices for excellent iOS apps
4
star
20

StoryboardDemo

A demo project to show how to use DATASource and DATAStack with Storyboards
Swift
4
star
21

Dream

The future of networking and persistency on iOS, OS X, watchOS and tvOS
Swift
4
star
22

JSON

JSON made so simple, it hurts
Swift
4
star
23

SyncDemo

A simple demo of how to set up and use Sync to fetch data from the network and display it in a UITableView
Swift
2
star
24

NestedXibs

Sample project on how to embed a xib inside another xib.
Swift
2
star
25

OfflineCRUDDemo

Swift
2
star
26

ios-mvc

Example of Model-View-Controller in iOS instead of Model-View-ViewController
Swift
2
star
27

Pinwheel

Swift
2
star
28

Toyay

To Do iOS app
Swift
2
star
29

chromeless-ios

An app that wraps any website in a native container
Swift
1
star
30

Notification

Swift
1
star
31

SimulatorCheck

Check for code running on the Xcode simulator
Ruby
1
star