title: How to restrict textfield to accept only decimal values in swift
body: <p>I want to accept only decimal values in my textfield.</p>
  
  <p>The following code allows me to enter only numbers and '.' but we can enter more than one '.'</p>
  
  <p>How can i restrict it to just one '.' or how can I restrict the textfield to accept only decimal values in swift 3.1</p>
  
  <pre><code>let aSet = NSCharacterSet(charactersIn:"0123456789.").inverted
  let compSepByCharInSet = r_Qty_txt.text?.components(separatedBy: aSet)
  let numberFiltered = compSepByCharInSet?.joined(separator: "")
  </code></pre>
  
  <p>My target device is iPad.</p>
  
  <p>listViewCell.swift Code</p>
  
  <pre><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) -&gt; 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!)! &gt; 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
  }
  }
  </code></pre>