How to force a dot for decimal pad

I have a UITextField that I am using to enter IP Addresses. I am using the Decimal Pad keyboard for this field.


IP Addresses are dotted-decimal in all regions (to best of my knowledge). I want to force the "decimal" on the pad to be DOT regardless of region. I have searched here and found several bug reports, but no answer.


On another site I saw a recommendation to subclass UITextField to deliver a UITextInputMode.primaryLanguage that keeps DOT. I found that UITextField does call for UITextInputMode, but it never queries the "primaryLanguage" of that mode (so overriding it doesn't help).


I've also seen some reports that say that the decimal is determined by the Region rather than the language (and I don't know how to override region for a UITextField).


How do I make the decimal stay a DOT for UITextField? Thanks!

A simple way is to force replace "," sign with "."


Use

optional func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


If you want to avoid "," char:


    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "," {
            textField.text = textField.text! + "."
            return false
        }
        return true     // Could also filter on numbers only
}

You have to set the delegate of the textField to the class.

If you have several textFields, need to check this is the right one.

A simple way is to define a tag for the testFields.


So code should look like:

let textFieldIPAddressTag = 1
let textField2Tag = 2    // May choose more explicit names…

// You set the tag in IB in property inspector

// You set the delegate in connections inspector


// If the textField for IP address is with tag 1


    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.tag == textFieldIPAddressTag {
             if string == "," {
                 textField.text = textField.text! + "."
                 return false
             }
       }
        return true     // We accept all typing otherwise ; but you could also filter on numbers only
    }

Thanks Claude; I am already doing something similar, but this doesn't solve the user experience problem.


The field clearly shows that it should be dotted-decimal format and they know it should be dotted-decimal format, but the only thing they have on their Decimal pad is a comma. This leads them to confusion.


I want the keypad to actually show a DOT.

It's a bit of a kludge but you might be able to add a UILabel or UIImageView on top of the keypad in just the right location to cover the "," with a "."

What is your locale ? en_US ? or en or en_GB ? To get decimal point on keyboard, seems you need en_US

Have a look here

https://stackoverflow.com/questions/51233517/get-decimal-separator-of-decimal-pad-keyboard-on-ios

It doesn't matter what MY locale is. I need the decimal keypad to function as if the USER's locale is en-US regardless of what her locale is...and I can't (shouldn't) change the user's locale. Basically I want to override their locale for one UITextField. How do I do that?

I have exactly the same problem.. I have tried many solutions found in various forums but nothing seem to work for iOS13..


I have a UITextField that should accept IP address (so only numbers and "." dots), it works well in english language but if i set it in italian (for example) it shows a comma instead of the dot. My UITextField also validates the IP address so the comma obviosly doesn't pass the validation.


Very frustrating!

This is how I bypassed the problem until a real solution is found... IT WORKS WITH iOS 13


I created a function that checks the current locale of the device and sets the keyboards accordingly. So for "en_GB" and 'en_US" it will display a Decimal keyboard (with the dot) and for anything else it will show a different keyboard (in my case I choose "numbersAndPunctuation").


func SetCorrectDecimalKeyboard(UITextField: UITextField)  {
   
    let language = Locale.current.identifier
   
    if (language == "en_GB" || language == "en_US") {
       
        print("[LOG] \(language) - Setting Decimal keyboard")
        UITextField.keyboardType = UIKeyboardType.decimalPad
    }
    else {
       
        print("[LOG] \(language) - Setting Numbers & Punctuation keyboard")
        UITextField.keyboardType = UIKeyboardType.numbersAndPunctuation
    }
}


You can more languages that support the decimal keyboard in this function if needed.


Usage:


SetCorrectDecimalKeyboard(UITextField: myTextFieldName)  
//Replace myTextFieldName with the name of your UITextField that you want to have a different keyboard for



While this doesn't resolve your issue I hope it helps you bypass it my friend! 🙂

textField.keyboardType = .decimalPad //Gives numbers 1,2,3..9 and decimal "."

This solution from @Jxa13 is good but we need to know every locale which have dot in decimalPad or comma in order to make code work properly for everyone. If I find something with this case I'll post this here

How to force a dot for decimal pad
 
 
Q