简体   繁体   English

如何限制文本字段快速接受十进制值

[英]How to restrict textfield to accept only decimal values in swift

I want to accept only decimal values in my textfield. 我只想在文本字段中接受十进制值。

The following code allows me to enter only numbers and '.' 以下代码允许我仅输入数字和“。” but we can enter more than one '.' 但是我们可以输入多个'。'。

How can i restrict it to just one '.' 我如何将其限制为仅一个'。 or how can I restrict the textfield to accept only decimal values in swift 3.1 或者如何限制文本字段在Swift 3.1中仅接受十进制值

let aSet = NSCharacterSet(charactersIn:"0123456789.").inverted
let compSepByCharInSet = r_Qty_txt.text?.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet?.joined(separator: "")

My target device is iPad. 我的目标设备是iPad。

listViewCell.swift Code listViewCell.swift代码

import UIKit

class listViewCell: UITableViewCell, UITextFieldDelegate {

var delegate: CellInfoDelegate?

@IBOutlet weak var desc_lbl: UILabel!
@IBOutlet weak var openQty_lbl: UILabel!
@IBOutlet weak var r_Qty_txt: UITextField!
@IBOutlet weak var itemId_lbl: UILabel!

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let newString: String = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    let expression: String = "^[0-9]*((\\.|,)[0-9]{0,2})?$"
    //var error: Error? = nil
    let regex = try? NSRegularExpression(pattern: expression, options: .caseInsensitive)
    let numberOfMatches: Int = (regex?.numberOfMatches(in: newString, options: [], range: NSRange(location: 0, length: (newString.characters.count ))))!
    return numberOfMatches != 0
}


public func configure(textVal: String?, placeholder: String){
    r_Qty_txt.text = textVal
    r_Qty_txt.placeholder = placeholder

    r_Qty_txt.accessibilityValue = textVal
    r_Qty_txt.accessibilityLabel = placeholder

}

@IBAction func QtyEntered(_ sender: UITextField) {
    print("Value Added \(String(describing: r_Qty_txt.text)) and \(String(describing: openQty_lbl.text))")
    if (r_Qty_txt.text!.isEmpty) {

    }else if(Int(r_Qty_txt.text!)! > Int(openQty_lbl.text!)!){
        print("Not Allowed")
        r_Qty_txt.text = nil
    }
}

override func awakeFromNib() {
    r_Qty_txt.delegate = self
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

If you want to allow just decimal number with your textField you can simply make it like this way no need to compare anything else. 如果您想在textField中只允许使用十进制数字,则可以像这样简单地使其无须进行其他比较。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.text != "" || string != "" {
        let res = (textField.text ?? "") + string
        return Double(res) != nil
    }
    return true
}

select keyboard type just like this from atribute inspector for only numbers. 从属性检查器中仅选择数字,就像这样选择键盘类型。

尝试此代表仅一个点

and use delegate for only one decimal points(.) 并仅使用代表小数点(。)

func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool
{
    let countdots = textField.text.componentsSeparatedByString(".").count - 1

    if countdots > 0 && string == "."
    {
        return false
    }
    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM