swift之TextField神魔禁止系列

手机号输入:禁止输入除数字以外的符号

身份证:只能输入'x','X',加数字

发红包:只能输入一个小数点,且只能保存两位小数

使用注意,要判断小数点,或者x时,必须先将TextFieldLimit.shareIntance.isHavePoint/isHaveX赋值为false

依照了点点点系列方法

// MARK: UITextView代理方法
extension PhoneVerificationView: UITextFieldDelegate {
    
    func textField(
        textField: UITextField,
        shouldChangeCharactersInRange range: NSRange,
        replacementString string: String) -> Bool {
        
       return  TextFieldLimit
        .shareIntance
        .textField(textField)
        .range(shouldChangeCharactersInRange: range)
        .string(replacementString: string)
        .phoneNumber()
        &&
        TextFieldLimit
        .shareIntance
        .length(6)
    }
    
}

 

TextFieldLimit.swift

//
//  InputNumberAndPoint.swift
//  DPplatform
//
//  Created by 王雄皓 on 16/7/29.
//  Copyright © 2016年 王雄皓. All rights reserved.
//

import UIKit

public class TextFieldLimit: NSObject {

    static let shareIntance = TextFieldLimit()

    // 每次视图实例都必须重新赋值
    var isHavePoint = false
    var isHaveX     = false
    // 限制最大值
    private let maxNumber: Int32 = 99999
    
    
    private var textField = UITextField()
    private var range = NSRange()
    private var string = String()
    
    /*
     * 需要限制的textField
     * @parm tf
     * return self
     */
    public func textField(tf: UITextField) -> TextFieldLimit{
        
        textField = tf
        return self
    }
    
    /*
     * @parm range
     * return self
     */
    public func range(shouldChangeCharactersInRange rang: NSRange) -> TextFieldLimit {
        
        range = rang
        return self
    }
    
    /*
     * @parm str 最后拼接字符
     * return self
     */
    public func string(replacementString str: String) -> TextFieldLimit {
        
        string = str
        return self
    }
    
    /*
     * 限制只能输入数字,点,不能输入特殊字符
     * return true / false
     */
    public func numberAndPoint() -> Bool {
        
        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        
        let length = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        let dianRange = newString.rangeOfString(".")
        
        if dianRange == nil{
            isHavePoint = false
        }
        
        // 限制只能输入数字,点,不能输入特殊字符
        for loopIndex in 0..<length {
            let char = (string as NSString).characterAtIndex(loopIndex)
            if isHavePoint == false {
                
                if char == 46 {
                    
                    isHavePoint = true
                    return true
                }
            }
            if char < 48 { return false }
            if char > 57 { return false }
        }
        
        if isHavePoint {
            
            let dianIndex = Int((dianRange?.description[0...0])!)
            if newString.length - 3 > dianIndex {
                return false
            }
        }
        
        // 最大值
        let trueMoney = NSString.init(string: newString)
        if trueMoney.intValue > maxNumber {
            return false
        }
        
        return true
    }
    
    /*
     * 限制输入长度
     * @param long 限制长度
     * return true / false
     */
    public func length(long: Int) -> Bool {
        
        let text: NSMutableString = NSMutableString.init(string: textField.text!)
        text.replaceCharactersInRange(range, withString: string)
        return text.length <= long
    }
    
    /*
     * 限制输入长度
     * @param long 限制长度
     * return true / false
     */
    public func currentText() -> String {
        
        let text: NSMutableString = NSMutableString.init(string: textField.text!)
        text.replaceCharactersInRange(range, withString: string)
        return text as String
    }
    
    /*
     * 验证输入手机号码
     * @param num 手机号码字符串
     * return true / false
     */
    public func telNumber(num: String) -> Bool {
        
        let mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
        let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
        let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$"
        let CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@",CM )
        let regextestcu = NSPredicate(format: "SELF MATCHES %@" ,CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@" ,CT)
        
        if ((regextestmobile.evaluateWithObject(num) == true)
            || (regextestcm.evaluateWithObject(num)  == true)
            || (regextestct.evaluateWithObject(num) == true)
            || (regextestcu.evaluateWithObject(num) == true))
        {
            return true
        }
        else
        {
            return false
        }
    }
    
    /**
     *  验证身份证号码是否正确的方法
     *  param IDNumber 传进身份证号码字符串
     *  return 返回true或false表示该身份证号码是否符合国家标准
     */
    public func isCorrect(IDNumber: String?) -> Bool {
        
        let IDs = NSMutableArray()
        let IDNumberNSString = IDNumber! as NSString
        
        for index in 0...16 {
            
            let range = NSMakeRange(index, 1)
            let subString: Int = IDNumberNSString.substringWithRange(range).toInt()!
            IDs.addObject(subString)
        }
        
        let coefficientArray = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        let remainderArray = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]
        
        var sum = 0
        for index in 0...16 {
            
            let coefficient = coefficientArray[index] as Int
            let ID = IDs[index] as! Int
            sum = sum + coefficient * ID
            print(sum)
        }
        
        let theCalculateLastNumber = remainderArray[sum % 11]
        
        let theLastNumber = IDNumberNSString.substringFromIndex(17).toInt()
        
        if theCalculateLastNumber == theLastNumber {
            return true
        }else if theLastNumber == nil && (theCalculateLastNumber == "x" || theCalculateLastNumber == "X")  {
            return true
        }
        
        
        return false
    }
    
    /**
     *  限制只能输入数字
     *  return true或false
     */
    func phoneNumber() -> Bool {
        
        let length = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        
        // 限制只能输入数字,点,不能输入特殊字符
        for loopIndex in 0..<length {
            let char = (string as NSString).characterAtIndex(loopIndex)
            if char < 48 { return false }
            if char > 57 { return false }
        }
        return true
    }
    
    /**
     *  限制只能输入数字和X且只有一个X
     *  return true或false
     */
    func IDCard() -> Bool {
      
        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        
        let length = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        let xRange = newString.rangeOfString("x")
        let XRange = newString.rangeOfString("X")
        
        if XRange == nil && xRange == nil{
            isHaveX = false
        }
        
        // 限制只能输入数字,点,不能输入特殊字符
        for loopIndex in 0..<length {
            let char = (string as NSString).characterAtIndex(loopIndex)
            if isHaveX == false {
                
                if char == 88 || char == 120{
                    
                    isHaveX = true
                    return true
                }
                
            }
            if char < 48 { return false }
            if char > 57 { return false }
        }
        
        return true
        
    }
    
    
}

 

posted on 2016-08-18 17:33  XHShare  阅读(790)  评论(0编辑  收藏  举报

导航