- Home /
Restricting input field to predefined words
Hi Unity Community
I am wanting to use Unity’s UI Input Field as a means for searching predefined words, aided by autocomplete prompts. The user input needs to be restricted only to valid search terms (perhaps defined in a generic dictionary or what ever is appropriate).
Can anyone provide me any direction as to how this may be achieved w/o resorting to purchasing assets?
Many thanks in advance,
Ryan
Hi @Cherno, thanks for your response. How could I use the char class to cross-reference against a predefined word list?
I understand I could set up a whole lot of if statements using the char class to detect (in)correct input, however this could get pretty messy in the case of a long list of valid terms so I am wondering if there is a cleaner and more efficient way of achieving this? Also, ideally this would allow for valid terms to be added to the list during runtime.
I don't think a lot of if statements are needed, just one or two loops that check if the list of stored strings contains the currently entered string/group of chars.
I think you make it more user friendly, if you let the user write anything they want and just give a list with entrys from your database wich contains the input. So the user can chose, from this list. Just like google does it. Or am I understanding you wrong?
Hi @Oribow
I think you’re understanding me right, thanks! You actually helped clarify for me my UI needs.
The searching functionality is intended to allow users to searching the text contents of various elements in an X$$anonymous$$L document. In some cases there may be only be as few as four available terms, others there may be many.
What I really want is to allow the users to press button of type of X$$anonymous$$L field they want to search > this pulls all available terms from X$$anonymous$$L to GUI > user can click one of these terms for a return | or | the user can search for their own defined term (restricted by the char class approach described above - prevents the user wasting their time searching for unavailable terms ) > this then pulls available term results to screen
Answer by Oribow · Oct 03, 2015 at 03:50 PM
Try this code out. It will restrict wrong input, and it gives you a list with all matching words.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WordSearch : MonoBehaviour
{
//All words you want your user to pick from
string[] dictionary = new string[] { "Hallo", "Input", " Abde", "Hey", "kill" };
//the inputfield your user is typing to
public InputField inputField;
public bool caseSensetive;
public bool restrictUserInput;
ArrayList possibleWords;
void Start()
{
inputField.onValueChange.AddListener(delegate { ValueChangeCheck(); });
possibleWords = new ArrayList(dictionary.Length);
}
void ValueChangeCheck()
{
possibleWords.Clear();
//Go through all words in your databas (could be slow with many words)
for (int i = 0; i < dictionary.Length; i++)
{
//Check if the words start matches the start from the input
if (dictionary[i].StartsWith(inputField.text, !caseSensetive, null))
possibleWords.Add(i);
}
if (possibleWords.Count == 0 && restrictUserInput)
inputField.text = inputField.text.Remove(inputField.text.Length -1,1);
// Iterate through all possible words
for (int i = 0; i < possibleWords.Count; i++)
{
Debug.Log(dictionary[(int)possibleWords[i]]); // do something with the word
//may display it
}
}
}