- Home /
How to use onValidateInput in JavaScript?
The following UnityScript code, applied to an InputField in Unity 4.6, is adapted from the documentation:
#pragma strict
private var field : UI.InputField;
function Start () {
field = GetComponent(UI.InputField);
field.onValidateInput() += function() {
return validateDot(addedChar);
};
}
function validateDot (ch : char) {
return "X"[0]; //(Test character to show validation is working)
}
It fails with two errors:
BCE0005: Unknown identifier: 'addedChar'.
And (presumably a side effect of the first error):
BCE0051: Operator '+' cannot be used with a left hand side of type 'function(String, int, char): char' and a right hand side of type 'function(): error'.
What's the proper way to add my own character validation to the default validation?
Thanks.
Answer by erlanter · Aug 16, 2015 at 06:44 PM
I bumped into the same problem and used this workaround, adapted from the documentation you referenced:
#pragma strict
// Required when Using UI elements.
public class Example {
public var mainInputField : InputField;
public function Start() {
// Sets the MyValidate method to invokes after the input field's default input validation invokes (default validation happens every time a character is entered into the text field.)
mainInputField.onValidateInput = function (text : String, charIndex : int, addedChar : char) {
return MyValidate(addedChar);
};
}
private function MyValidate (charToValidate : char) {
//Checks if a dollar sign is entered....
if (charToValidate == '$'[0]) {
// ... if it is change it to an empty character.
charToValidate = '\0'[0];
}
return charToValidate;
}
}
My workaround does not "+=" the function, which throws an error I can't grok. I am noob.
Your answer
Follow this Question
Related Questions
[4.6 UI][JS] Focusing on InputField issues. 1 Answer
Cable-building simulations. 0 Answers
How to delete new'd arrays? 2 Answers
How to create random movement in 2D 2 Answers
Get all Game Objects near point 2 Answers