Inputfield return key and move on

Hello

I have a inputfield and a button below.

inputfield is for player name and that works great.
When pressing enter the name gets stored but remains on same page. that’s why I created a button to go to next page.

I want to remove the button so when user enters the name and click enter/return then move to next page.
How do I make so the name has to be filled or else you can’t move to next page…
thanks

Hello,
That is some basic “form” verifications. Usually, incredibly good programmers will use regular expressions, to check if it’s a proper e-mail and so on.
In your case, you can simply make an “if” statement, checking if the input field is empty or made only of white spaces. Depends on your architecture, but you can even put it in Update :

string txt = textComponent.text; // to replace with your own component
txt = txt.Replace(" ", System.String.Empty); // to remove any spaces

if (txt == "") // check if empty once we removed the white spaces
        button.enabled = false;
else
        button.enabled = true;
// enables or disables the button if conditions are met

Don’t forget to null check and so on.

1 Like

I don’t need the button.
Just a input field, when user enter some text and press enter, they move on but the field canot be empty…
If that makes since…

Is there not a way with the input field visual options?

I should have read better, my bad.
Instead of activating / deactivating a button, you can check for Input.GetKeyDown.

if(Input.GetKeyDown(KeyCode.KeypadEnter))
{
if(txt == "")
  MoveOn();
}

Maybe you can check for values standarts with Content Type in Input Field Settings if you’re using TMPro. But I’m not sure you can check a form and perform those actions without a little bit of scripting. If you’re really allergic to c#, maybe check with UIToolkit but being in the “scripting” section I have assumed you wanted some piece of code :wink:

you can put MoveOn() into the OnEndEdit section, but beware, it would also move on when using tab and other keys.

Ok I will go the script way, but right now I just have a UI inputfield. On that i have 2 scripts attached to the “On End edit”.
They hold the create user name and store it and then the other script is a public void move to next panel.
So do i create a new script and attach it to the inputield??

Below is code to store the the username. It shoud be in here somewhere?

public void CreateUserName()
    {
        //Create a save method like a button to store the nane
        playerNameText.text = playerInputField.text;
        PlayerPrefs.SetString("user_name", playerNameText.text);
        PlayerPrefs.Save();

    }

I tried to due it with bools.

SO that if something has been entered it sets it to true and then can move on.
But now after i enter a name or anything and press enter, nothing happens…

 public void CreateUserName()
    {
        if (Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            if (playerInputField.text == "")
                storeName();
            userNameEntered = true;

        }

      

    }


    public void storeName()
    {


       
        //Create a save method like a button to store the nane
        playerNameText.text = playerInputField.text;
        PlayerPrefs.SetString("user_name", playerNameText.text);
        PlayerPrefs.Save();


    }

And here is the code for the panel to be open if the condition is true

 public void introThree()
    {

        if(gs.userNameEntered == true)
        {

            panelIntro3.SetActive(true);
            panelIntro.SetActive(false);
            panelStart.SetActive(false);
            Time.timeScale = 0f;
            GameIsPaused = true;

        }

Ok…
Thanks …
I got it now :slight_smile:
That NULL saved med…