• Stars
    star
    311
  • Rank 134,521 (Top 3 %)
  • Language
    Objective-C
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated almost 9 years ago

Reviews

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

Repository Details

Simple and ready to use subclass of UITextField which uses NSRegularExpression to allows you to validate text in the text field with pattern you set.

TSValidatedTextField

Simple and ready to use subclass of UITextField that allows you to validate TextField text value with pattern you set.

imageimage

Donate

If you like it... :)

Flattr this

CocoaPods

You can install it via CocoaPods.

pod 'TSValidatedTextField', '~> 1.0'

Properties

regexpPattern - String pattern which will be used to valid value from the field.

regexp - NSRegularExpression object that you can pass to use your RegularExpression object instead of default one.

regexpValidColor - Color for valid text value.

regexpInvalidColor - Color for invalid text value.

Colors aren't necessary, you can set block instead and do other things after field validation.

validatedFieldBlock - Block which return ValidationResult enum value. It's not set by default. You may set it, but it's not necessary. It should be used for more sophisticated things during validation than changing textField color (use regexpValidColor and regexpInvalidColor instead).

validWhenType - Default set to YES. If you set it to NO value will be validated when editing is done. E.g. after switch to next field.

looksForManyOccurences - If set to YES field will be validated and validator will be looking for one or more occurencies in the value. It should be used with occurencesSeparators property described below.

occurencesSeparators - This value should be set only if you are using looksForManyOccurences. This array store separators which user can use to separate content in the field. E.g. user typed numbers "20, 30,40, 50". If occurencesSeparators is set to @[",", ", "] the value will be validated successful. Property can simplify pattern.

numberOfCharactersToStartValidation - Field is validate when its value will be equal or longer than set number. If the text is shorter than this value the field looks normal (without valid and invalid colors - start state) but if block has been defined it will be called with ValueTooShortToValidate. Default set to 1 (minimum value).

isValid - (readonly, BOOL) return YES if value in field is valid, otherwise NO.

How to use it?

There are few ways to use this class.

1) Colored text field

    _fullNameTextField.regexpPattern = @"[a-zA-Z]{2,}+(\\s{1}[a-zA-Z]{2,}+)+"; /// e.g. Tomasz Szulc or Cing Yo Ciong
    _fullNameTextField.regexpValidColor = [UIColor validColor];
    _fullNameTextField.regexpInvalidColor = [UIColor invalidColor];

Regexp pattern and both valid and invalid colors have been set. TextField is validating when user typing.

2) TextField with validation block

    _phoneNumberTextField.regexpPattern = @"\\d{3}-\\d{3}-\\d{3}"; /// e.g 555-343-333
    _phoneNumberTextField.minimalNumberOfCharactersToStartValidation = 11;
    _phoneNumberTextField.validWhenType = NO;
    _phoneNumberTextField.validatedFieldBlock = ^(ValidationResult result, BOOL isEditing) {
      
        switch (result) {
            case ValidationPassed:
                NSLog(@"Field is valid.");
                break;
                
            case ValidationFailed:
                NSLog(@"Field is invalid.");
                break;
                
            case ValueTooShortToValidate:
                NSLog(@"Value too short to validate. Type longer");
                break;
        }
    };

No colors set (if valid or invalid the textColor will be still the same). validWhenType property has been set to NO so field will be validated when user end editing. Instead of change color of text block will be called with one case of ValidationResult enum.

3) Validation after few characters

    _wordStartsWithA.regexpPattern = @"^A[a-zA-Z]{3,}";
    _wordStartsWithA.regexpValidColor = [UIColor validColor];
    _wordStartsWithA.regexpInvalidColor = [UIColor invalidColor];
    
    /// Visualization of validation will be visible when value is 4 characters long.
    _wordStartsWithA.minimalNumberOfCharactersToStartValidation = 4;

Text field will be validated if length of text will be equal or longer than minimalNumberOfCharactersToStartValidation. If block has been configured it will be called with parameter TooShortToValidate value.

4) TextField check for many occurences

    _separatedNumbersByComma.regexpPattern = @"[-]?[0-9]+";
    _separatedNumbersByComma.regexpValidColor = [UIColor validColor];
    _separatedNumbersByComma.regexpInvalidColor = [UIColor invalidColor];
    _separatedNumbersByComma.looksForManyOccurences = YES;
    _separatedNumbersByComma.occurencesSeparators = @[@",", @", "];

The value looksForManyOccurences and occurencesSeparators have been set here. If field wants numbers it can be validate successful if user type "20, 20, 40, 60" or "20,20,40,60" or "20, 20,40, 60" too. It's strongly recommended to use looksForManyOccurences and occurencesSeparators together.

5) TextField with own NSRegularExpression

    _wordStartsWithB.regexpValidColor = [UIColor validColor];
    _wordStartsWithB.regexpInvalidColor = [UIColor invalidColor];
    
    NSString *pattern = @"^B[a-zA-Z]{3,}";
    NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    [_wordStartsWithB setRegexp:regexp];

If default NSRegularExpression isn't sufficient you can set custom.

More Repositories

1

Swifternalization

Localize iOS apps in a smarter way using JSON files. Swift framework.
Swift
579
star
2

fireworks

Fireworks for UIView
Swift
340
star
3

NetworkLayerExample

Demo project for the article: http://szulctomasz.com/how-do-I-build-a-network-layer/ (Swift 3)
Swift
255
star
4

TSAssetsPickerController

Highly customizable control used to select multiple assets (saved photos and videos) for use in your app.
Objective-C
219
star
5

TSFileCache

Generic class that allows you to cache files on device's disk. Need to cache images or other files? Check this. Easy to subclass and reuse.
Objective-C
98
star
6

TSActivityIndicatorView

Very simple view class that looks like an UIActivityIndicatorView but is fully customizable. It's great for you if you want to add indicator view to you game or app and it should be customized.
Objective-C
70
star
7

Quotes

Swift app containing iOS 8/9 features and things mentioned on WWDC 2015
Swift
25
star
8

2048

2048 written in Swift in 10 hours and shipped to the App Store
Swift
23
star
9

demo-xcconfig

Demo that shows how to use xcconfig files
Swift
23
star
10

citybike-app

City Bike - Find Free Bike - Swift - Open Source app
Swift
16
star
11

undo-manager-practice

Simple app that presents how to use NSUndoManager to undo and redo user's actions
Swift
12
star
12

cloudkit-demo-goatstagram

Very simple demo that shows how to check account status, fetch and save subscriptions and fetch objects from the backend
Swift
12
star
13

TSTextMapper

It maps text inside UILabel and allows you to detect tapped words
Swift
11
star
14

python-fav-memes-rest-api

RESTful Web Service written in Python/Flask for storing and getting your favourite memes
Python
7
star
15

eslpod-downloader

Python script which downloads available episodes of http://eslpod.com podcast. ~27GB
Python
5
star
16

TSCircleView

A subclass of MKCircleView which supports scaling and animating.
Objective-C
5
star
17

UnusedStrings

Helps Cocoa applications localization by detecting unused keys in '.strings' files.
Shell
4
star
18

Endpoints

Super simple library to describe endpoints for your network layer
Swift
3
star
19

ExpandingViewTransition-Demo

Example of how to create custom view controllers transition
Swift
3
star
20

Response

Tiny class for carrying a network response. You might want to use it with tomkowz/Endpoints
Swift
3
star
21

szulctomasz.com

Personal blog. #ios #swift #objective-c #programming
CSS
2
star
22

MemesBrowser-Network-Layer-Demo

Demo of a new Network Layer architecture I learned recently.
Swift
1
star
23

imessage-memes-extension

Simple extension for iMessage that provides my favourite memes from my VPS
Swift
1
star
24

Nothing-App

"To Do" iOS Swift App created for the Master's Thesis
Swift
1
star
25

twiliovoice

Python
1
star
26

transitioning-button-demo

For my blog. Button like control with nice transitions
Swift
1
star
27

arduino-eeprom-example

Example of using EEPROM
Makefile
1
star