If text field is empty then set button disabled to false else to true

So i’m trying to create a text field and a button and if the text field is empty the button will be disabled and if there is a value in the text field then the button disabled will be set to enabled. This is what I have tried…

[lua]

–textListener

local function textListener( event )

    if ( event.phase == “began” ) then

        – user begins editing defaultField

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        – do something with defaultField text

if textbox1.text == nil or textbox1.text == “”

then btnSend:isEnabled(false)

elseif btnSend:isEnabled(true)

    elseif ( event.phase == “editing” ) then

    end

end

–textbox1

local textbox1 = native.newTextField( 160, 130, 300, 40 )

textbox1.size = 20

textbox1:addEventListener( “userInput”, textListener )

–btnSend

local btnSend = widget.newButton

    {

        width = 150,

        height = 40,

        defaultFile = “btnSend.png”,

        onEvent = onbtnSend,

    }

    btnSend.x = 160

    btnSend.y = 200

    btnSend.isEnabled = false

[/lua]

Thanks again,

Matt.

Hi Matt,

You’re on the right track, but you need to wrap your text listener conditions a bit differently. See the example in the documentation on what event properties are available in each event phase:

http://docs.coronalabs.com/api/library/native/newTextField.html

Also, your button is not in the proper scope here. You have it locally scoped below the text listener function which will attempt to set it as enabled or not. So, Lua will not know what object you’re talking about.

Take care,

Brent

Hi Brent,

I have made some changes to my textListener but I am still getting error messages any ideas on what I’m doing wrong.

[lua]

function textListener( event )

    if ( event.phase == “began” ) then

        – user begins editing defaultField

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        – do something with defaultField text

        if event.text == nil or event.text == “” then

        btnNext:setEnabled( true )

    elseif ( event.phase == “editing” ) then

    end

end

[/lua]

Thanks again,

Matt.

Hi Matt,

Please inspect the documentation example carefully. In the “ended” or “submitted” phases, it’s event .target.text for the reference to the text field’s content. You have it as simply event.text.

Best regards,

Brent

Hi Brent,

Thanks for clearing that up, I still seem to be getting an error message saying this…

Runtime error

error loading module ‘riskAssessmentPg1’ from file ‘c:\users\matt\documents\corona projects\hs app\riskAssessmentPg1.lua’:

c:\users\matt\documents\corona projects\hs app\riskAssessmentPg1.lua:199: ‘end’ expected (to close ‘function’ at line 12) near ‘<eof>’

stack traceback:

[C]: in function ‘error’

?: in function ‘gotoScene’

c:\users\matt\documents\corona projects\hs app\forms.lua:22: in function ‘_onPress’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221>

My Text Listener now looks like this…

[lua]

local function textListener( event )

    if ( event.phase == “began” ) then

        – user begins editing defaultField

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        – do something with defaultField text

        if event.target.text == nil or event.target.text == “” then

      --btnNext:setEnabled( true )

  print( “its working” )

    elseif ( event.phase == “editing” ) then

    end

end

[/lua]

Any ideas on what’s going wrong, I have put my (btnNext:setEnabled(true) in a comment for now and just tried to get it to print “its working” in the console but still nothing.

Thanks again,

Matt.

Hi Matt,

This is a simple error where you haven’t “end”-ed your conditional block. Out of curiosity, have you read the “Debugging Guide” which has plenty of advice on how to figure out what error messages mean and how to pinpoint them? Of course you can ask in the forums but I suggest you study the guide thoroughly:

http://docs.coronalabs.com/guide/basics/debugging/index.html

Best regards,

Brent

Hi Brent,

Thanks for clearing that up for me. I will definitely take a good look at that guide you posted.

Matt.

Hi Brent,

This might be a silly question but I can’t find out how to do it, is there a way to test that a text field has input inside as I can test if its empty but not sure how to test with an inputted value.

Thanks again,

Matt.

Hi Matt,

If you mean checking the opposite of the text field being empty (something inside it) just do a ~= check on an empty string ("").

Brent

Hi Brent,

Is this how you the code would look for this…

[lua]

function OnbtnNext( event )

    if textBoxName.text == nil or textBoxName.text == “” then

    print( “No Input in Text Field.” )

    end

elseif textBoxName.text ~= “” then

   composer.gotoScene( “riskAssessmentPg2”, { effect=“slideLeft”, time=800,} ) 

end

end

[/lua]

Thanks again,

Matt.

That looks right, but you’ve ended the if before the elseif, so that will give you an error. Get rid of that first “end” and I think you’ll be fine.

Also, I think after you check whether the text is equal to nil or “”, there’s no need to check whether it’s not equal to “” as this must obviously be the case, so you can just use else instead of elseif.

Hi Brent,

That has worked perfectly.

Thanks again,

Matt.

Hi Brent,

Just a quick one, every so often it posts two alerts any idea on why this is?

Thanks,

Mattt.

Are you sure this is the right thread? I don’t see anything about alerts above. Could you post the code with the alert in it? And the code that calls it.

It sounds like your code is reacting to an event with multiple phases.

This is my full code.

[lua]


– incidentReportingPg1.lua


local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

function onComplete( event )

   if event.action == “clicked” then

        local i = event.index

        if i == 1 then

        end

    end

end

function scene:create( event )

local sceneGroup = self.view

–bg

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 ) – white

sceneGroup:insert ( bg )

–titleHeader

local titleHeader = display.newImage( “titleHeader.png” )

titleHeader:translate( 160, -22 )

–title

local title = display.newText( “Incident Reporting”, 0, 0, native.systemFont, 32 )

title:setFillColor( 0 ) – black

title.x = display.contentWidth * 0.5

title.y = -25

–textPage

local textPage = display.newText(“Page 22”, 

280,130, display.contentWidth -25 , display.contentHeight * 0.5, native.systemFont, 15 )

textPage:setFillColor( 0, 0, 0 )

– Handle press events for the buttons

local function onSwitchPress( event )

    local switch = event.target

    print( “Switch with ID '”…switch.id…"’ is on: "…tostring(switch.isOn) )

end

– Image sheet options and declaration

local options = {

    width = 26,

    height = 26,

    numFrames = 2,

    sheetContentWidth = 26,

    sheetContentHeight = 52

}

local checkboxSheet = graphics.newImageSheet( “checkboxSheet.png”, options )

– Create a group for the radio button set

local radioGroup = display.newGroup()

– Create two associated radio buttons (inserted into the same display group)

local radioButton1 = widget.newSwitch

{

    left = 20,

    top = 100,

    style = “radio”,

    id = “RadioButton1”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton1 )

sceneGroup:insert( radioButton1 )

local radioButton2 = widget.newSwitch

{

    left = 20,

    top = 140,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton2 )

sceneGroup:insert( radioButton2 )

local radioButton3 = widget.newSwitch

{

    left = 20,

    top = 180,

    style = “radio”,

    id = “RadioButton3”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton3 )

sceneGroup:insert( radioButton3 )

–textCheck1

local textCheck1 = display.newText(“textCheck1”, 

210, 225, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck1:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck1 )

–textCheck2

local textCheck2 = display.newText(“textCheck2”, 

210, 265, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck2:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck2 )

–textCheck3

local textCheck3 = display.newText(“textCheck3”, 

210, 305, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textCheck3:setFillColor( 0, 0, 0 )

sceneGroup:insert( textCheck3 )

function OnBtnNext( event )

    if radioButton1.isOn == true or radioButton2.isOn == true or radioButton3.isOn == true then

    composer.gotoScene( “riskAssessmentPg2”, { effect=“slideLeft”, time=800,} ) 

    

elseif radioButton1.isOn == false and radioButton2.isOn == false and radioButton3.isOn == false then

local alert = native.showAlert( “Warning”, “ERROR: No input in checkbox(s)”, { “OK” }, onComplete)

elseif radioButton1.isOn == true then

textCheck1 = myData.IRb1

else

print( myData.IRb1 )

end

end

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = OnBtnNext

}

btnNext.x = 235

btnNext.y = 445

local function home( event )

    composer.gotoScene( “forms”, { effect=“slideRight”, time=800} )

end

local btnHome = widget.newButton

    {

        width = 320,

        height = 50,

        defaultFile = “home.png”,

        onEvent = home,

    }

    btnHome.x = 160

    btnHome.y = 499

–sceneGroup

sceneGroup:insert( titleHeader )

sceneGroup:insert( title )

sceneGroup:insert( textPage )

sceneGroup:insert( btnNext )

end 

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

OnBtnNext is being called at every event phase. You only want it to be called at one. The most natural one is the ended phase, so in your OnBtnNext function, only perform the operations if event.phase == “ended”

Hi Matt,

You’re on the right track, but you need to wrap your text listener conditions a bit differently. See the example in the documentation on what event properties are available in each event phase:

http://docs.coronalabs.com/api/library/native/newTextField.html

Also, your button is not in the proper scope here. You have it locally scoped below the text listener function which will attempt to set it as enabled or not. So, Lua will not know what object you’re talking about.

Take care,

Brent

Hi Brent,

I have made some changes to my textListener but I am still getting error messages any ideas on what I’m doing wrong.

[lua]

function textListener( event )

    if ( event.phase == “began” ) then

        – user begins editing defaultField

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        – do something with defaultField text

        if event.text == nil or event.text == “” then

        btnNext:setEnabled( true )

    elseif ( event.phase == “editing” ) then

    end

end

[/lua]

Thanks again,

Matt.

Hi Matt,

Please inspect the documentation example carefully. In the “ended” or “submitted” phases, it’s event .target.text for the reference to the text field’s content. You have it as simply event.text.

Best regards,

Brent

Hi Brent,

Thanks for clearing that up, I still seem to be getting an error message saying this…

Runtime error

error loading module ‘riskAssessmentPg1’ from file ‘c:\users\matt\documents\corona projects\hs app\riskAssessmentPg1.lua’:

c:\users\matt\documents\corona projects\hs app\riskAssessmentPg1.lua:199: ‘end’ expected (to close ‘function’ at line 12) near ‘<eof>’

stack traceback:

[C]: in function ‘error’

?: in function ‘gotoScene’

c:\users\matt\documents\corona projects\hs app\forms.lua:22: in function ‘_onPress’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221>

My Text Listener now looks like this…

[lua]

local function textListener( event )

    if ( event.phase == “began” ) then

        – user begins editing defaultField

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        – do something with defaultField text

        if event.target.text == nil or event.target.text == “” then

      --btnNext:setEnabled( true )

  print( “its working” )

    elseif ( event.phase == “editing” ) then

    end

end

[/lua]

Any ideas on what’s going wrong, I have put my (btnNext:setEnabled(true) in a comment for now and just tried to get it to print “its working” in the console but still nothing.

Thanks again,

Matt.