在iOS上进行UITextField验证的更好方法是什么?

时间:2019-03-27 07:09:21

标签: ios swift swift4

我必须对名为RSFloatInputView的库中使用的ui文本字段进行验证。

这是我的臂架

import UIKit
import RSFloatInputView

class TextInputLayout: UIView {

    @IBOutlet weak var revealButton: UIButton!
    @IBOutlet weak var warningLabel: UILabel!
    @IBOutlet weak var rsFloatingView: RSFloatInputView!
    var contentView: UIView?

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    func xibSetup() {
        contentView = loadViewFromNib()
        contentView!.frame = bounds
        contentView!.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
        addSubview(contentView!)
    }

    func loadViewFromNib() -> UIView! {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "TextInputLayout", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        revealButton.tintColor = Color.grayColor()
        warningLabel.textColor = UIColor.red

        return view
    }  
}

当我单击下一步按钮

时,我想在此视图控制器中实现此功能
import UIKit
import DLRadioButton

class SecureWalletViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var securityPinStackView: UIStackView!
    @IBOutlet weak var securityPin: TextInputLayout!
    @IBOutlet weak var confirmSecurityPin: TextInputLayout!

    @IBAction func onNextButtonTap(_ sender: Any) {      

    }

    func textInputLayout(at index:Int) -> TextInputLayout {
        return securityPinStackView.arrangedSubviews[index] as! TextInputLayout
    } 
}

4 个答案:

答案 0 :(得分:0)

 Iam using this 
   // add func in swift class
    struct validatorConstants{
      static let errorMsg = "your error messages"
      static let customMsg = "your error messages"
      static let emailMsg =  "your error messages"
      }
  class Validators: NSObject {

     //MARK: Validation on any Empty TextField
   func validators(TF1:UITextField,errorMsg:String = validatorConstants.errorMsg,fieldName:String = "") -> Bool {
      var error = validatorConstants.errorMsg
            if fieldName.count > 0 {
              error =  fieldName + " is missing"
            }
            if  TF1.text?.isEmpty == true{
               kAppDelegate.showNotification(text: error)
              return false
            }
            return true
          }

          //MARK: Validation on any Email TextField
          func validatorEmail(TF1:UITextField,errorMsg:String = validatorConstants.errorMsg ,errorMsgEmail:String = validatorConstants.emailMsg,fieldName:String = "Email" ) -> Bool {
            var error = validatorConstants.errorMsg
            if fieldName.count > 0 {
                error =  fieldName + " is missing"
            }

            if  TF1.text?.isEmpty == true{
              kAppDelegate.showNotification(text: error)
              return false
            }
            if  TF1.text?.isValidEmail == false{
               kAppDelegate.showNotification(text: errorMsgEmail)
              return false
            }
            return true
          }
        }

   // call this func like this
  // your ViewController 
      var validator:Validators!
  // In viewdidload
      validator = Validators()
  // call this func on button Action
     guard validator.validators(TF1: txtfied,fieldName: "your txtfield name") == false
           else
          {
    //do something what you want
      return
    }
    // Its works for me hope its works for you

答案 1 :(得分:0)

我建议使用具有2个UI状态(常规/无效)和验证规则(例如,不为空/匹配正则表达式等)的UITextField子类

class ValidationTextField: UITextField {

    enum ValidationRule {
        case notEmpty
//      case matchRegex(regex: NSRegularExpression)
//      ...
    }

    var validationRule: ValidationRule?

    private(set) var isValid:Bool = true {
        didSet {
            updateUIForCurrentState()
        }
    }

    // Call this method on the "next" button click 
    // (or from the delegate on the textFieldDidEndEditing event for early validation)
    func validate() {
        guard let rule = validationRule else {
            // nothing to validate
            return;
        }
        switch rule {
        case .notEmpty:
            if let length = text?.count {
                isValid = length > 0
            }
            else {
                isValid = false
            }
        // process other cases (e.g. matchRegex)
        }
    }

    /// Configure your state-specific layout here.
    private func updateUIForCurrentState() {
        // Current implementation adds a red border in case of invalid input
        if isValid {
            layer.borderWidth = 0
            layer.borderColor = nil
        }
        else {
            layer.borderWidth = 2
            layer.borderColor = UIColor.red.cgColor
        }
    }
}

答案 2 :(得分:0)

您可以使用ASP.NET Core Service lifetimes,它是基于规则的验证器。

let validator = Validator()

   //Register the fields that you want to validate
    validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])


@IBAction func signupTapped(sender: AnyObject) {
    validator.validate(self)
}

答案 3 :(得分:0)

UITextFieldDelegate方法进行验证,如下所示:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true
    }

或使用自定义验证功能Here