1

In my app, there is a place where the user has to enter a decimal into a text field. However, I do not know how to restrict a text field to only valid decimals. I tried an example here, but the text field still lets me input more than one decimal point, lets me put a decimal point in the beginning of the text field, and it doesn't let me delete characters. How do you restrict a text field to only valid decimals? Thanks!

Community
  • 1
  • 1
Aleksandr
  • 13
  • 4
  • check this quation : http://stackoverflow.com/questions/12944789/allow-only-numbers-for-uitextfield-input – Sangram Shivankar Mar 20 '16 at 10:17
  • http://stackoverflow.com/questions/10734959/uitextfield-should-accept-number-only-values or http://stackoverflow.com/questions/20122455/uitextfield-allow-only-numbers-and-punctuation-input-keypad – Sangram Shivankar Mar 20 '16 at 10:19

3 Answers3

1
 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{

let textFieldString = textField.text! as NSString;

var newString = textFieldString.stringByReplacingCharactersInRange(range, withString:string)

let floatRegEx = "^([0-9]+)?(\\.([0-9]+)?)?$"

let floatExPredicate = NSPredicate(format:"SELF MATCHES %@", floatRegEx)

return floatExPredicate.evaluateWithObject(newString)


}
Rashid
  • 829
  • 7
  • 6
0

In the link that you have provided in the question the code checks only for one decimal place. You have to modify your code as follows. I am quoting the code from the link that you provided

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.delegate = self
    }

    //Textfield delegates
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // return false to not change text
        // max 2 fractional digits allowed
        let newText = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        let regex = try! NSRegularExpression(pattern: "\\..{3,}", options: [])
        let matches = regex.matchesInString(newText, options:[], range:NSMakeRange(0, newText.characters.count))
        guard matches.count == 0 else { return false }

        switch string {
        case "0","1","2","3","4","5","6","7","8","9":
            return true
        case ".":
            let array = textField.text?.characters.map { String($0) }
            var decimalCount = 0
            for character in array! {
                if character == "." {
                    decimalCount++
                }
            }
            if decimalCount > 0 {
                return false
            } else {
                return true
            }
        default:
            let array = string.characters.map { String($0) }
            if array.count == 0 {
                return true
            }
            return false
        }
    }
}

Hope this helps

Jobins John
  • 1,391
  • 25
  • 41
  • Please note that this code will not work with users from regions where the dot isn't the decimal separator. They'll be unable to enter non-integer values – FredericP Mar 20 '16 at 08:00
0

Here's what I've tried and works for me.

// TextField Delegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if string.isEmpty { return true }
    let currentLocale = NSLocale.currentLocale()
    let decimalSeparator = currentLocale.objectForKey(NSLocaleDecimalSeparator) as! String
    let existingTextHasDecimalSeparator = textField.text?.rangeOfString(decimalSeparator)
    let replacementStringHasDecimalSeparator = string.rangeOfString(decimalSeparator)
    let allowedCharactersSet = NSCharacterSet(charactersInString: decimalSeparator + "0123456789")
    let allowedCharacters = string.rangeOfCharacterFromSet(allowedCharactersSet)

    if existingTextHasDecimalSeparator != nil && replacementStringHasDecimalSeparator != nil || allowedCharacters == nil
    {
        return false
    }
    else
    {
        return true
    }
}
Muhammad Hassan
  • 1,027
  • 1
  • 8
  • 22