1
\$\begingroup\$

I am using the code below to ask users for their username.

MyTextInputListener listenerz = new MyTextInputListener();
Gdx.input.getTextInput(listenerz, "Create Username", "");

I have knowledge in PHP but beginner in Java LibGdx. I know that in HTML there is a REQUIRED

<input name='username' required />

That's why I wanted to know the equivalent coding in LibGdx for the REQUIRED input.

\$\endgroup\$
  • \$\begingroup\$ Can't you just check yourself if the input String is not empty? \$\endgroup\$ – Cedric Martens Jul 3 '17 at 19:50
  • \$\begingroup\$ In HTML, the user won't be able to submit the form if the required input is empty, no need for validation. I just wonder if LibGdx has this kind of functionality. \$\endgroup\$ – Jam Ville Jul 3 '17 at 19:54
  • \$\begingroup\$ the user won't be able to submit the form if the required input is empty this is true, but the user can easily remove the required field in html with his browser, so you should always do backend verification regardless. \$\endgroup\$ – Cedric Martens Jul 3 '17 at 19:56
3
\$\begingroup\$

If you want to check if the user entered a valid input you could do something like this:

if(input == null || input.length() < minLength) {
    //Invalid input. Do something.
    //You might just ask the user for input again until it is valid.
    MyTextInputListener listenerz = new MyTextInputListener();
    Gdx.input.getTextInput(listenerz, "Create Username", "");
}

You should also add that code in the canceled() method of the listener:

@Override
public void canceled () {
    MyTextInputListener listenerz = new MyTextInputListener();
    Gdx.input.getTextInput(listenerz, "Create Username", "");
}

This way the input window will just repeat itself if invalid input is given.

\$\endgroup\$
  • \$\begingroup\$ your idea is great, if the input is empty then i should pop-up again the Text Input. But i should do nothing when it was CANCELED. Thanks \$\endgroup\$ – Jam Ville Jul 3 '17 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.