Deepak
Deepak

Reputation: 575

How to validate Text mesh pro input field text in unity

I have tried to use text mesh pro input field in my project but I have face one series issue with that. i.e If try to validate empty or null text in input field it fails. For example when user without typing any text in tmp input field and click done button I have set a validation like not allowed to save null empty values but when user click done button without typing any text, those validations are fails. Please suggest any idea to fix this issue. Thanks in advance.

Here is the code I have tried :

 var text = TextMeshProText.text;  // here "TextMeshProText" is 'TMP_Text'

 if(!string.IsNullOrEmpty(text))
 {
    //do required functionality
 }
 else
 {
    // Show alert to the user.
 }

I have set the validation like this but without giving any text click on done button it fails null or empty condition and enter in to if.

Upvotes: 3

Views: 12072

Answers (1)

Ehsan Mohammadi
Ehsan Mohammadi

Reputation: 1228

I found the problem. It fails because you use TMP_Text instead TMP_InputField.

Note that: Use the code for TMP_InputField; not for TMP_Text that is inside it as a child.

Change your code to this:

TMP_InputField TextMeshProText;

...

public void OnClick () 
{
   var text = TextMeshProText.text;  // here "TextMeshProText" is 'TMP_InputField'

   if (!string.IsNullOrEmpty(text))
   {
       //do required functionality
   }
   else
   {
       // Show alert to the user.
   }    
}

I hope it helps you

Upvotes: 6

Related Questions