TextField Validation library for Swift (@IBDesignable & Programmatically)
First create your custom validator class.
The class has to have @objc declaration, conform to YMRulesValidator protocol and NSObject .
@objc(EmailValidator)
class EmailValidator: NSObject, YMRulesValidator {
var regex: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
}- Add your TextField
- Set the custom class
YMValidator - On attributs inspector, set
Class NameandError Message
- Set the error label to
YMValidator
@IBOutlet weak var errorEmailLabel: UILabel!
@IBOutlet weak var inputEmail: YMValidator!
override func viewDidLoad() {
super.viewDidLoad()
self.errorEmailLabel.text = ""
self.inputEmail.setErrorLabel(self.errorEmailLabel)
}//ViewController.swift
let textField = YMValidator(frame: CGRect(x: 30.0, y: 296.0, width: 540, height: 30), rulesValidator: CustomValidator(), errorMessage: "Only alphanumeric characters are allowed", errorLabel: customErrorLabel)
self.view.addSubview(textField)You can check if all inputs are valid with this static function.
YMValidator.areValid(self)
To run the example project, clone the repo, and run pod install from the Example directory first.
- iOS 8.0+
- Xcode 8 (Swift 3)
YMValidator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "YMValidator"If using Xcode 8, paste the following at the bottom of your Podfile, updating the Swift version if necessary:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '2.3' # or '3.0'
end
end
endDrag and drop YMValidator.swift into your project.
@YMonnier - contact@yseemonnier.com
We are open to any proposals to improve this library!!
The best way to contribute is by submitting a pull request(Add comments in your code before to it). We'll do our best to respond to your patch as soon as possible. You can also submit a new GitHub issue if you find bugs or have questions. ![]()
YMValidator is available under the MIT license. See the LICENSE file for more info.


