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

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”