This seems like a bug to me. There is no indication that the character limit property should be ignored when using a custom validator. I typically want to validate the characters being input - for many different fields - but the character limit of each one may be different.
Hi,
I would suggest opening a bug report and attaching a sample project. Please provide the ticket# once you submitted the bug report and the team will take a look.
Kind of interesting I was linked to this thread and see a familiar face ![]()
Anyway, I had the same issue. That issue being this section of code inside TMP_InputField:
if (onValidateInput != null)
{
input = onValidateInput(validateText, insertionPosition, input);
}
else if (characterValidation == CharacterValidation.CustomValidator)
{
input = Validate(validateText, insertionPosition, input);
if (input == 0) return;
SendOnValueChanged();
UpdateLabel();
return; // <- This prevents Insert from being called which checks characterLimit
}
else if (characterValidation != CharacterValidation.None)
{
input = Validate(validateText, insertionPosition, input);
}
// If the input is invalid, skip it
if (input == 0)
return;
// Append the character and update the label
Insert(input);
My currently unapproved hack is to store a reference to the input field using the OnSelect InputField event to continue using the CustomValidator but this requires several extra steps that should instead work out of the box.
Bug: https://fogbugz.unity3d.com/default.asp?1403543_t20b17ld448ek0bg
@Stephan_B have you seen this? It’s been around for a while.
I take a look as soon as possible.
Hi, I tried using latest version, but this issue was not resolved.
Why has the code for characterLimit been commented out in TMP_InputField.cs for two years?
TMP_InputField.cs
version : Changelog | TextMeshPro | 3.2.0-pre.10
void SetText(string value, bool sendCallback = true)
{
~~~
/*
if (m_LineType == LineType.SingleLine)
value = value.Replace("\n", "").Replace("\t", "");
// If we have an input validator, validate the input and apply the character limit at the same time.
if (onValidateInput != null || characterValidation != CharacterValidation.None)
{
m_Text = "";
OnValidateInput validatorMethod = onValidateInput ?? Validate;
m_CaretPosition = m_CaretSelectPosition = value.Length;
int charactersToCheck = characterLimit > 0 ? Math.Min(characterLimit, value.Length) : value.Length;
for (int i = 0; i < charactersToCheck; ++i)
{
char c = validatorMethod(m_Text, m_Text.Length, value[i]);
if (c != 0)
m_Text += c;
}
}
else
{
m_Text = characterLimit > 0 && value.Length > characterLimit ? value.Substring(0, characterLimit) : value;
}
*/
~~~
}
For anyone still searching for a solution for this, I ended up dealing with it by just adding a check for the length of the string in the custom input validators Validate override, and returning if it’s over the specified limit.
public override char Validate(ref string text, ref int pos, char ch)
{
// Don't allow the string to be longer than 64 characters
if (text.Length >= 64)
{
return (char)0;
}
if (char.IsLetterOrDigit(ch) || (char.IsWhiteSpace(ch) && !PreviousCharacterIsWhiteSpace(text, pos) && !NextCharacterIsWhiteSpace(text, pos)))
{
text = text.Insert(pos, ch.ToString());
pos++;
return ch;
}
return (char)0;
}