Is there a way to detect if a number or letter is typed?

I’m trying to detect if a number or letter was typed.
And if a letter was type, an alert would pop up.
Is there a way to do this?
Thank you!
here’s my beginner’s attempt with error messages… =0/

[code]
local numbersTyped = {1,2,3,4,5,6,7,8,9,0}

local function letterTypedAlert( event )
if numbersTyped = myField.text then
print (“yes, numbers are good”)
else
print(“Please key in a number.”)

end
end

myField = native.newTextField( 10, 10, 180, 30 )
myField:addEventListener( “userInput”, letterTypedAlert )
myField.text = “0”

[/code] [import]uid: 169633 topic_id: 32287 reply_id: 332287[/import]

Yea i think u can, not sure but lookin at your code you are tryin to compare a table to a string, that’s why you are getting errors,

Havent tested it, but try with this:

[lua]local numbersTyped = {1,2,3,4,5,6,7,8,9,0}

local function letterTypedAlert( event )
for i=1,#numbersTyped do
if numbersTyped[i] == myField.text then
print (“yes, numbers are good”)
else
print(“Please key in a number.”)

end
end
end

myField = native.newTextField( 10, 10, 180, 30 )
myField:addEventListener( “userInput”, letterTypedAlert )
myField.text = “0”[/lua] [import]uid: 139893 topic_id: 32287 reply_id: 128422[/import]

Thanks for your help Encodi.fm!
I tried it out and whatever key I pressed- number or letter, it only printed the “Please key in a number.” message.

I’m not sure if the numbers need to be in a table or is there another way to express number input. With my current experience, I thought scripting it in a table would be most logical.

Is there any other way to express number input or letter input?

Thanks all! And thanks again encodi! [import]uid: 169633 topic_id: 32287 reply_id: 128453[/import]

no problem, and you’re in the right track… im not completly sure but the problem could be the if statement condition that is comparing a number with a string…lets see…

try testing something like this:

[lua]local numbersTyped = {1,2,3,4,5,6,7,8,9,0}

local function letterTypedAlert( event )
for i=1,#numbersTyped do
if numbersTyped[i] == tonumber(myField.text) then
print (“you pressed”…myField.text)
else
print(“Please key in a number.”)
end
end
end

myField = native.newTextField( 10, 10, 180, 30 )
myField:addEventListener( “userInput”, letterTypedAlert )
myField.text = “0”[/lua] [import]uid: 139893 topic_id: 32287 reply_id: 128481[/import]

Yea i think u can, not sure but lookin at your code you are tryin to compare a table to a string, that’s why you are getting errors,

Havent tested it, but try with this:

[lua]local numbersTyped = {1,2,3,4,5,6,7,8,9,0}

local function letterTypedAlert( event )
for i=1,#numbersTyped do
if numbersTyped[i] == myField.text then
print (“yes, numbers are good”)
else
print(“Please key in a number.”)

end
end
end

myField = native.newTextField( 10, 10, 180, 30 )
myField:addEventListener( “userInput”, letterTypedAlert )
myField.text = “0”[/lua] [import]uid: 139893 topic_id: 32287 reply_id: 128422[/import]

It’s giving me the same result.
I’ll try to research and work on it more.

Thanks again for your help! [import]uid: 169633 topic_id: 32287 reply_id: 128540[/import]

If you check for event.newCharacters instead, does it work?

I’m thinking if you start with “0”, after adding a character, myField.text will be something like “00” or “01”… [import]uid: 27791 topic_id: 32287 reply_id: 128551[/import]

Thanks for your help Encodi.fm!
I tried it out and whatever key I pressed- number or letter, it only printed the “Please key in a number.” message.

I’m not sure if the numbers need to be in a table or is there another way to express number input. With my current experience, I thought scripting it in a table would be most logical.

Is there any other way to express number input or letter input?

Thanks all! And thanks again encodi! [import]uid: 169633 topic_id: 32287 reply_id: 128453[/import]

no problem, and you’re in the right track… im not completly sure but the problem could be the if statement condition that is comparing a number with a string…lets see…

try testing something like this:

[lua]local numbersTyped = {1,2,3,4,5,6,7,8,9,0}

local function letterTypedAlert( event )
for i=1,#numbersTyped do
if numbersTyped[i] == tonumber(myField.text) then
print (“you pressed”…myField.text)
else
print(“Please key in a number.”)
end
end
end

myField = native.newTextField( 10, 10, 180, 30 )
myField:addEventListener( “userInput”, letterTypedAlert )
myField.text = “0”[/lua] [import]uid: 139893 topic_id: 32287 reply_id: 128481[/import]

Thanks for you help StarCrunch!
I tried the even.newCharacters and for the “0” in myField.text, I changed “0” to “” so it starts out blank. But it returned similar results. [import]uid: 169633 topic_id: 32287 reply_id: 128609[/import]

It’s giving me the same result.
I’ll try to research and work on it more.

Thanks again for your help! [import]uid: 169633 topic_id: 32287 reply_id: 128540[/import]

If you check for event.newCharacters instead, does it work?

I’m thinking if you start with “0”, after adding a character, myField.text will be something like “00” or “01”… [import]uid: 27791 topic_id: 32287 reply_id: 128551[/import]

Thanks for you help StarCrunch!
I tried the even.newCharacters and for the “0” in myField.text, I changed “0” to “” so it starts out blank. But it returned similar results. [import]uid: 169633 topic_id: 32287 reply_id: 128609[/import]