- Home /
Change InputField decimal mark
The default decimal mark of a "Decimal Number" InputField is '.' How do you change it to ',' (comma) depending on the language?
Sorry, I should have been more clear. I am talking about mobile devices where pressing the "comma" button on the keyboard does nothing, because the InputField only accepts numbers and a decimal mark which is '.'
Answer by Fornoreason1000 · Jun 02, 2015 at 12:44 PM
Actually its pretty simple, or complicated, depending on how "accurate" you want to be. What you want would be Regular Expressions. the reason I say regular repressions is instead of just String.Replace is so you don't just replace every "." such as full stops to ",".
Basically with decimals you have a a number after and before the decimal like this
0.2938498 or 111.348569
its up to you if you want to allow these
.03 0.999125
We wanbt to filer anything that isn't either a number or a "," / "." so either way your regular expression will look like this.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Text.RegularExpressions;
public class EURODecimalInput : MonoBehaviour {
//Set thius to true if you want to use commas instead of fullstops
public bool useComma = false;
/// <summary>
/// This will get called by the OnTextChanged Event
/// Make sure you use the Dynamic Text version not the verison that will let you edit the string
/// </summary>
/// <param name="input"></param>
public void ValidateStringCharacters(string input)
{
//get out input feild, even though we just fired an event from it
InputField InputField = GetComponent<InputField>();
//assign our regular expression string, it will be different if you use commas hence the "if"
string regexp = @"[^0-9\.]?";
if (useComma)
{
//commas don't neeed escaping
regexp = @"[^0-9,]?";
}
//Assign out regex Object
Regex regex = new Regex(regexp);
//Replace anything that isn't a number with empty
input = regex.Replace(input, "");
//print(input);
//Apply our Validated string
InputField.text = input;
}
}
OnValitadateString() is fired from the On Value Change event on your Input field. You will also need to change your Input type to "Standard" or it will not work correctly.
Sorry for the confusion Earlier,
Hope it helps
Sorry, I should have been more clear. I am talking about mobile devices where pressing the "comma" button on the keyboard does nothing, because the InputField only accepts integers and a decimal mark which is '.' I want to be able to change this depending on the selected (or device) language.
ok... more or less the same , but you have to somehow hook the input string. or you could do the character validation manually. I'm not sure what Unity does but you can use Regular expressions for that. Hold on I'll change my Answer.
I did something similar to your solution using InputField.onValidateInput, but the problem is that Unity does not allow for a comma to be entered, when the keyboardType is NumbersAndPunctuation or NumberPad. Even when setting the characterValidation to none the default input validation still invokes and removes anything other than digits or punctuations. This applies to mobile platforms only.
It seems right now the only solution is to make a custom InputField.
I left the content type to Standard If your worried about the mobile keyboard thing, you might just make an inbuilt one in your game