Limiting user input to a valid decimal number in Swift
Here is a simple example:
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 NO to not change text
switch string {
case "0","1","2","3","4","5","6","7","8","9":
return true
case ".":
let array = Array(textField.text)
var decimalCount = 0
for character in array {
if character == "." {
decimalCount++
}
}
if decimalCount == 1 {
return false
} else {
return true
}
default:
let array = Array(string)
if array.count == 0 {
return true
}
return false
}
}
}
How to limit the textfield entry to 2 decimal places in swift 4?
Set your controller as the delegate for the text field and check if the proposed string satisfy your requirements:
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.keyboardType = .decimalPad
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let oldText = textField.text, let r = Range(range, in: oldText) else {
return true
}
let newText = oldText.replacingCharacters(in: r, with: string)
let isNumeric = newText.isEmpty || (Double(newText) != nil)
let numberOfDots = newText.components(separatedBy: ".").count - 1
let numberOfDecimalDigits: Int
if let dotIndex = newText.index(of: ".") {
numberOfDecimalDigits = newText.distance(from: dotIndex, to: newText.endIndex) - 1
} else {
numberOfDecimalDigits = 0
}
return isNumeric && numberOfDots <= 1 && numberOfDecimalDigits <= 2
}
Limit Text Field to one decimal point input, numbers only, and two characters after the decimal place - Swift 3
You need to assign delegate to your textfield and in the shouldChangeCharactersIn delegate method do your validations:
Add extension with validation methods for the string:
extension String{
private static let decimalFormatter:NumberFormatter = {
let formatter = NumberFormatter()
formatter.allowsFloats = true
return formatter
}()
private var decimalSeparator:String{
return String.decimalFormatter.decimalSeparator ?? "."
}
func isValidDecimal(maximumFractionDigits:Int)->Bool{
// Depends on you if you consider empty string as valid number
guard self.isEmpty == false else {
return true
}
// Check if valid decimal
if let _ = String.decimalFormatter.number(from: self){
// Get fraction digits part using separator
let numberComponents = self.components(separatedBy: decimalSeparator)
let fractionDigits = numberComponents.count == 2 ? numberComponents.last ?? "" : ""
return fractionDigits.characters.count <= maximumFractionDigits
}
return false
}
}In your delegate method:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Get text
let currentText = textField.text ?? ""
let replacementText = (currentText as NSString).replacingCharacters(in: range, with: string)
// Validate
return replacementText.isValidDecimal(maximumFractionDigits: 2)
}
How to limit decimal input value in UITextField
You probably need two checks:
Make sure it in the form of
xxx.xx
. This sort of pattern matching is often achieved by using regular expression search.The trick here is to make sure you support all permutations with and without decimal place, where the fractional digits is two or fewer digits and the integer digits is three or fewer digits.
Try converting it to a number and check that the value is less than 999.
Thus:
let formatter = NumberFormatter()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let candidate = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
let separator = formatter.decimalSeparator!
if candidate == "" { return true }
let isWellFormatted = candidate.range(of: "^[0-9]{1,3}([\(separator)][0-9]{0,2})?$", options: .regularExpression) != nil
if isWellFormatted,
let value = formatter.number(from: candidate)?.doubleValue,
value >= 0,
value < 999 {
return true
}
return false
}
Note:
I’m assuming you want users to be able to honor their device’s localization settings (e.g. let a German user enter
123,45
because they use,
as the decimal separator).The regular expression,
"^[0-9]{1,3}([\(separator)][0-9]{0,2})?$”
probably looks a little hairy if you’re not used to regex.- The
^
matches the start of the string; - The
[0-9]
obviously matches any digit; - The
{1,3}
matches between one and three integer digits; - The
(...)?
says “optionally, look for the following”; - Again,
[0-9]{0,2}
means “between zero and two fractional digits; and - The
$
matches the end of the string.
- The
How to limit number of decimals in textfield when user input is from pasteboard, iOS Swift
This code always updates the content of textField
but limits the number of decimals:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let decimalSeparator = NSLocale.current.decimalSeparator else {return true}
// Updates the text
var updatedText = (textView.text as NSString).replacingCharacters(in: range, with: text)
// If someone needs to cover all possible decimal separator values, the commented line below is the solution
// let textComponents = updatedText.components(separatedBy: [",", "."])
let textComponents = updatedText.components(separatedBy: decimalSeparator)
// Truncates the decimals
if textComponents.count > 1 && textComponents[1].count > 6{
updatedText = textComponents[0].appending(decimalSeparator).appending((textComponents[1] as NSString).substring(to: 6))
}
textView.text = updatedText
// The text has already been updated, so returns false
return false
}
How can I limit the number of decimal points in a UITextField?
Implement the shouldChangeCharactersInRange method like this:
// Only allow one decimal point
// Example assumes ARC - Implement proper memory management if not using.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *arrayOfString = [newString componentsSeparatedByString:@"."];
if ([arrayOfString count] > 2 )
return NO;
return YES;
}
This creates an array of strings split by the decimal point, so if there is more than one decimal point we will have at least 3 elements in the array.
How to restrict user to type till 29 digits and take two decimals in swift?
Use textField delegate method to restrict input values, its called whenever the textfield text is updated/changed.
func textField(_ textField: UITextField, shouldChangeCharactersIn
range: NSRange, replacementString string: String) -> Bool
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if newString.characters.count > 32 { //restrict input upto 32 characters
return false
} else {
let characterset = CharacterSet(charactersIn: "0123456789.") //0-9 digit and . is allowed
if newString.rangeOfCharacter(from: characterset.inverted) == nil {
let fullNumberArray = newString.components(separatedBy: ".") //Convert string into array
if fullNumberArray.count > 2 { // more than 2 . exist
return false
}
else if fullNumberArray.count == 2 { // Fractional part exist
if fullNumberArray[0].characters.count <= 29 && fullNumberArray[1].characters.count <= 2 {
return true
} }else {
// Only No decimal point exist , numeric digits only entered so far
if fullNumberArray[0].characters.count <= 29 {
return true
}
}
}
}
return false
}
Note
Above code allows to enter maximum 29 numeric digits and maximum 2 fractional digits.User can type numbers without decimals .If you have a restriction of only allow 32 character and there is minimum and maximum 29 digit without fraction range then some conditions will be reduced.
Related Topics
Non Power of Two Textures in iOS
How to Use Crashlytics with iOS/Os X Today View Extensions
Url Scheme "Open Settings" iOS
Getting Button Action:Uicollectionview Cell
Swift "Bridging-Header.H" File Not Allowing Me to Instantiate Objective-C Classes in .Swift Files
Creating Thumbnail for Video in iOS
Allowing Single Digit in Uitextfield in iOS
How to Create and Save Ekcalendar on iOS 6
Limiting User Input to a Valid Decimal Number in Swift
Apns Notifications Not Reaching Devices Enrolled in Apple Mdm
Swift 3: Expression Implicitly Coerced from 'Uiview' to Any
Change Size of Uibarbuttonitem (Image) in Swift 3
Unified Uiviewcontroller "Became Frontmost" Detection
Can't Install Watchkit App on Apple Watch
Setting Contentoffset Programmatically Triggers Scrollviewdidscroll