Greater than variables

I am trying to check if the username that the user enters is greater than 10 but it’s not working . 

I’ve tried this :

local function usernameLength( event ) if username.text \> 10 then local alert = native.showAlert( "Username is to long ", "Your username cannot be longer than 130 characters",{ "Try again" } ) return true end end

local function usernameLength( event ) if (username.text \> 10) then local alert = native.showAlert( "Username is to long ", "Your username cannot be longer than 130 characters",{ "Try again" } ) return true end end

local function usernameLength( event ) if username.text \> 10 then local alert = native.showAlert( "Username is to long ", "Your username cannot be longer than 130 characters",{ "Try again" } ) return true else return false end end

https://docs.coronalabs.com/daily/api/library/string/len.html

It doesn’t show me how long I can make a variable 

if string.len(username.text) \> 10 then -- too long end

First post of 2017?  :slight_smile:

LOL it might be . But the code doesn’t work

You’ve been given everything you need to make it work, you can’t expect people to write your entire game for you, one thread at a time.

This is the current code I have that doesn’t work

local function usernameTooLong( event ) if string.len(username.text) \> 10 then local alert = native.showAlert( "username to long", "There was an error logging in.", { "Try again" } ) end end

@comeUp264,

I don’t understand why you’re having a problem applying the answers you’ve been given to your code.  Are you a very new programmer, very young, is english not your first language?

I am not picking on you. I am genuinely curious.  I have given you input on many occasions, but you never seem to be able to use that input and I don’t get the sense that you’re trying very hard to understand or figure things out.  

In order to make games on your own, you need to learn to program.  As a programmer, you must be flexible and curious.  However, if you are in fact young and/or inexperienced and/or english is not your first language, then I can better understand why this is so hard for you.

I want you and all other Corona developers to be successful, but I must know why straightforward answers to  problems are flying by you.

If you  tell us more about who you are and what level you’re at we may be better able to tailor our answers to help you.

Note for anyone reading this in the future: “Why did I ask about English?” I asked, because the docs and most examples are in English and this adds a significant barrier for English as a second, third, … , language folks.  I totally respect folks who don’t speak English (as their first language) trying to use Corona, and knowing that they face such a barrier helps me help them.

I’m young not very young (17) . I’m a beginner .

Cool, that gives us a place to start, 

next:

Is Lua your first programming language?  If not, what languages have you used before?

Is Corona your first SDK or have you worked with other game engines and SDKs?

Have you had any formal programming classes in school?

Mac or Windows User?  - Not super relevant, but it does help frame things a little.

No . The only languages I know how to use off the back is html ,css and a little PHP . I was using android studio and then I fount out than I can build games and my social network here . So here I am . No I never learned anything about programming in school .

Windows 7 32-bit

You are young ^^ but it’s never too early to learn programing

You need to understand, we are here only to help you. We win nothing in exchange

To learn programming Lua isn’t the better langage, lua don’t learn best practice or how it realy work. 

Have a nice day :slight_smile:

For information english is my third speak langage and I am 21

Roger that.  Thanks for answering.  I really do appreciate that and it helps me a lot.

Now, let me help a little with the original question, and then give some advice that should help you ramp up.

Original Question

I grabbed the first code block from your first post:

local function usernameLength( event ) if username.text \> 10 then local alert = native.showAlert( "Username is to long ", "Your username cannot be longer than 130 characters",{ "Try again" } ) return true end end

I can see immediately that there are a couple things wrong here:

  1. usernameLength(event) - This looks like it is a listener and not a function that is called by you directly.
  2. username.text - username is clearly a global which indicates you’re having trouble setting up safe and proper scope.

These observations tell me you are trying to write a text input listener, and you want it to stop the user from entering too many characters for the field.  

To do so, I would do this :

local defaultField local maxLen = 10 local function textListener( self, event ) -- Cap length at maxLen characters. If user types maxLen+1 characters, the end char -- will automatically be removed local text = event.text if( string.len(text) \> maxLen ) then -- Is the string longer than maxLen? If so... self.text = string.sub( text, 1, maxLen ) end end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField.userInput = textListener defaultField:addEventListener( "userInput" )

While it may seem like a good idea to pop-up a message to warn the user, it may not be a great idea because I’m not 100% sure how that will affect the text field (also a native object) interaction.  I think simply removing extra characters will be good enough.  The user will see this happening.

Advice

  1. Since you are  a new programmer and Lua is new to you - AND - because Lua is the foundation language for Corona users, you need to learn it before using Corona or writing games.  That would have helped a lot in this case and others.

I suggest you take a short break from your game and go read the PIL: https://www.lua.org/pil/contents.html

Specifically, I suggest you read and type in the example code for these chapters and sub-chapters:

  • 1 … 5 - Fundamentals
  • 11.1 - Arrays
  • 18 - math lib
  • 19 - table lib
  • 20 - string. library (would have helped with this question)
  • 21 - io.* library
  1. Grab a book and follow along.  I strongly suggest you read through at least one Corona Dev book (Try Dr. Burton’s site or Amazon)

  2. Take a programming class (any language).  If you get the opportunity to take a programming class at school, go for it.  This will help a lot.  I promise.  It is incredibly hard to write games and program starting from zero or nearly zero.  I strongly believe that all programmers should start with a formal education and then branch out.  Otherwise you risk having gaps in your knowledge that will hobble you for life.

4. Spend more effort on your responses and posts.

You may have noticed that some of  us (me included) get exasperated with you.  For me, this is because I spend real time and effort answering you.  I typically:

  • Type long clear answers
  • Use proper spelling, punctuation, sentence structure, paragraphs, …
  • I provide clear and well formatted code in code blocks.
  • Go back and re-edit me post if there is something wrong or poorly formatted
  • even make videos sometimes.

Then, you respond with a one-liner like, “It didn’t work.”

This makes me want to pull out my hair.  It also makes me NOT want to help you.  So, as my final word of advice.  Please realize that short answers like that are seen as disrespectful and entitled behavior and do not encourage folks to help.

Again, I want you and other Corona developers to be successful and I really want to help when I can, so as I’ve said in other places to other folks, “Help me to help you.”

Cheers,

Ed

When I reply with one line I am absolutely not trying to disrespect anyone , that’s not why i’m here . I’m not disrespecting anyone who is trying to help me and who also shows respect . So for me irritating you , thanks for not being disrespectful it says a lot .

Bonus.  If you have multiple text fields and you want to limit them each at different lengths you could do something like this:

 local function textListener( self, event ) if( string.len(self.text) \> self.maxLen ) then self.text = string.sub( self.text, 1, self.maxLen ) end end -- Pseudo-code follows -- local userName = native.newTextField( ... ) userName.maxLen = 10 userName.userInput = textListener userName:addEventListener( "userInput" ) local zipCode = native.newTextField( ... ) zipCode.maxLen = 6 zipCode.userInput = textListener zipCode:addEventListener( "userInput" ) local age = native.newTextField( ... ) age.maxLen = 3 age.userInput = textListener age:addEventListener( "userInput" )

I keep getting this error :

13: in function 'emptyFields' 71: in function '\_onEvent'

local function emptyFields( event ) if ( username.text == "" or pw.text == "" or pw2.text == "" or email.text == "" ) then --line 13 local alert = native.showAlert( "Error", "Fill in all fields .", { "Try again" } ) return true else return false end end 

local function userRegister( event ) if ( "ended" == event.phase ) then if emptyFields() == true then -- line 71

I don’t see anything specifically wrong w/ that code and the error message is vague w/o being able to see the full app.

You’ll probably need to zip up your entire project and let someone examine it and run it to debug the problem.  Unfortunately, seeing that code out of context makes it too hard to debug.  

PS - I’m taking the rest of the day off, so I won’t be responding more today.  

Happy New Year to all!  

https://docs.coronalabs.com/daily/api/library/string/len.html

It doesn’t show me how long I can make a variable