- Home /
The question is answered, right answer was accepted
How to validate the characters of the strings to the characters typing via mobile keyboard?
Hey guys i am working on the prototype like this:link text
how can i validate the characters like in this game. right now i am doing this
nameInputField.onValidateInput += delegate(string input, int charIndex, char addedChar) { return MyValidate (addedChar); };
private char MyValidate(char charToValidate)
{
//Checks if a dollar sign is entered....
if (charToValidate == textChar[i])
{
// ... if it is change it to an empty character.
//charToValidate = '\0';
//
}
else
{
charToValidate = '\0';
//index = i ;
//charToValidate = textChar[i-1] ;
}
return charToValidate;
}
And if he types wrong he is not allowed to type, Like if I need to type"Hello" , and user press 'e' key on the start then nothing should happen, User can only type 'h' then 'e' then 'l' ...... so on,
but this seems to be not working for me please Help! iam using unity 5 and its ui inputfield.
Answer by amanpu · Sep 02, 2015 at 06:42 AM
If I understand your question properly then following should work. I am using OnValueChange event of InputField.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Validate : MonoBehaviour {
private InputField inputField;
private string expectedString = "Hello";
// Use this for initialization
void Start () {
inputField = GameObject.Find ("InputField").GetComponent<InputField>();
}
// Update is called once per frame
void Update () {
}
public void OnValueChanged()
{
if (inputField.text.Length <= expectedString.Length ) // if length exceeds expectedString then we will remove current char
{
if(inputField.text.Length > 0) // this is for safe side if we remove everything from inputField
{
if (!inputField.text [inputField.text.Length - 1].Equals (expectedString [inputField.text.Length - 1])) //if current char is not same as expected char
{
inputField.text = inputField.text.Substring (0, inputField.text.Length - 1); // this statement will remove last char
}
}
} else {
inputField.text = inputField.text.Substring (0, inputField.text.Length - 1);
}
}
}