Checking for the presence of a Textarea field. If so, do not comment.
I need help with my code. I have a text area that should only appear when a radio button is selected. Then, whatever is entered into the text area must be submitted and saved to the database. I have a button two radio
:
If selected No
, a text area appears:
The code for this is shown below:
<h1>Accept or Decline Invitation</h1>
<%=txt.displayCustomContent( "accept_text","acceptsection" )%>
<fieldset >
<label>Will you be attending the <%=formFields.getValue("programName")%> in <%=formFields.getValue("destination")%> <%=formFields.getValue("programDates")%>?</label>
</fieldset>
<fieldset>
<input id="yes" type="radio" name="attending" class="show-hide" value="Yes" <%= formFields.getRadioValue("attending","Yes")%>/> Yes, I will attend. I have read the instructions and I will register now.
</fielset>
<fieldset>
<input id="no" type="radio" name="attending" class="show-hide" value="No" <%= formFields.getRadioValue("attending","No")%>/> No, I will not attend.
</fieldset>
<br/>
<div id="show-me" style="display:none;">
<fieldset>
<label>If you will not be attending, please provide a brief explanation below (maximum 40 characters).
<textarea name="declineReason" id="declineReason" cols="25" rows="3" onKeyDown="textCounter(this.form.declineReason,40);" onKeyUp="textCounter(this.form.declineReason,40);"><%= formFields.getTextAreaValue("declineReason")%></textarea>
</label>
</fieldset>
</div>
<%=txt.displayCustomContent( "accept_textbottom","acceptsectionbottom" )%>
<div id="registration-navigation">
<input name="" type="submit" value="next >>" class="button"/>
</div>
Now here's where my problem is. Even when I select Yes
, the textarea sends the information back and stores it in the database.
The code to save it is shown below:
if(formFields.exists("declineReason"))
{
awsl.setLoginComment("Decline reason is: " + formFields.getValue("declineReason"));
}
So my question is, how do I change it so that it only sends information to the database if there is text in the text area? I tried to check and see if the textarea was NULL in the if statement, but that doesn't seem to work.
source to share
To compose what @Stultuske said, I would recommend still checking the null state to avoid the possibility of a null pointer exception. But as they said, once it is created it will never return null, so you will have to check if it is empty, which essentially just checks the length of the text inside the field.
if(jTextArea.getText() == null || jTextArea.getText().isEmpty())
{
// your code here
}
source to share