如何限制文本字段快速接受十进制值 [英] How to restrict textfield to accept only decimal values in swift

查看:86
本文介绍了如何限制文本字段快速接受十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在文本字段中接受十进制值.

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 '.'

如何将其限制为仅一个'.或如何限制文本字段在Swift 3.1中仅接受十进制值

How can i restrict it to just one '.' or how can I restrict the textfield to accept only decimal values in swift 3.1

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

我的目标设备是iPad.

My target device is iPad.

listViewCell.swift代码

listViewCell.swift Code

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
}
}

推荐答案

如果您只想在 textField 中使用小数,则可以像这样简单地使其无须进行任何比较.

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.

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
}

这篇关于如何限制文本字段快速接受十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆