How do I make a required field for the text area?

I know in ColdFusion I can just write text fields like this to make them required.

<cfinput type="text" name="subject" required="yes" message="Please Enter The Subject Of Your News.">


But how do I make a textarea act the same way?
LVL 1
fixx17Asked:
Who is Participating?
 
pinaldaveCommented:
let us try again....
========================================================
page 1 = postnews.cfm


<cfform name="form" method="post" action="postto1.cfm">
<cfinput type="text" name="name" required="yes" message="Please Enter Your Name">
<cfinput type="text" name="subject" required="yes" message="Please Enter The Subject Of Your News.">
<textarea name="news" cols="60" rows="10"></textarea>
<input type="submit" name="Submit" value="Submit">
========================================================
page 2= postto1.cfm
<cfif isdefined('form.news') and (Len(Form.news) EQ 0 or form.news eq '')>
<cfset ErrorList = "Please fill out the textarea">
<cfelse>

<cfquery name="postit" DATASOURCE="news">
  insert into teamnews
  (name,
   subject,
   news )
   values
     ('#form.name#',
      '#form.subject#',
      '#form.news#')

</cfquery>
</cfif>
========================================================
0
 
mrichmonCommented:
It is really just done through javascript - so really someone could turn off javascript and submit your enite form blank or with invalid data.

You should be sure to use server side checking as well.

When you do that it looks like this:
<cfset ErrorList = "">

<!--- Radio Button --->
<cfif NOT IsDefined("Form.radioname")>
<cfset ErrorList = "Please select a choice from the radio button">
</cfif>

<!--- textbox/area --->
<cfif Len(Form.inputname) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
</cfif>


0
 
pinaldaveCommented:
Hi fixx17,
you can not use cfinput to work with the text area.
you can refer this web site for javascript text area validation
http://www.javascriptkit.com/javatutors/form6.shtml

Regards,
---Pinal
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
pinaldaveCommented:
Hi fixx17,
<form name="feedback" action="script.cgi" method="post" onSubmit="return checkform()">
<input type="text" name="name">
<input type="text" name="email">
<textarea name="comments"></textarea>
</form>

if (document.feedback.field.value == '') {
    return false;
}


http://www.yourhtmlsource.com/javascript/formvalidation.html


Regards,
---Pinal
0
 
fixx17Author Commented:
Ok , mrichmon

I've tried to use this.

<!--- textbox/area --->
<cfif Len(Form.inputname) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
</cfif>


I am not sure if I am suppose to use EQ 0

I used your code like this

<!--- textbox/area --->
<cfif Len(Form.news) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
</cfif>

But I get an error that says "Error with ELEMENT News"

I am using a textarea for people to insert news to the front page for our website.  The code <cfif Len(Form.news) EQ 0> looks like it's looking for a number, not text.

pinaldave

I see the javascirpt on the link you sent, but it explains how to do it if a parameter is missing.  Such as the "@" symbol for e-mails.  But it does not show how to check if anything is missing.  Sorry, I am not a java expert.

This is what I have right now.
<cfform name="form1" method="post" action="postto1.cfm">
      
      <table width="100%"  border="0">
        <tr>
          <td colspan="4"><span class="style14">Post News, Events and Updates </span></td>
        </tr>
        <tr>
          <td width="14%">&nbsp;</td>
          <td width="13%">&nbsp;</td>
          <td width="44%">&nbsp;</td>
          <td width="29%">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" valign="top"><span class="style17">Name</span><span class="style18">:</span></td>
          <td><cfinput type="text" name="name" required="yes" message="Please Enter Your Name"></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" valign="top"><span class="style17">Subject</span></td>
          <td>
            <cfinput type="text" name="subject" required="yes" message="Please Enter The Subject Of Your News.">
          </td>
          <td></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" valign="top"><span class="style17">News</span></td>
          <td><textarea name="news" cols="60" rows="10"></textarea></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Submit"></td>
          <td>&nbsp;</td>
        </tr>
      </table></cfform>


Any suggestions.


0
 
pinaldaveCommented:
Do this...

<cfif isdefined('form.news') and Len(Form.news) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
</cfif>

it should do the trick!
0
 
pinaldaveCommented:
the reason is says it is undefined as you have not submitted the form yet... that is why...
the other method is you define that as default param like
<cfparam name="form.news" default="" type="string">

but that will not do the trick in your case as you want to validate that and if you keep it null it will always show up... also that is bad practice to declare as form.news  so use the previous method...
---Pinal
0
 
fixx17Author Commented:
Pinal

I've tried this.

<script>
function checkform()
{
      if (document.form.news.value == ' ') {
      return false;
}

</script>

<cfform name="form1" method="post" action="postto1.cfm" onSubmit="return checkform()">

It still will pass no information to the database and nothing gets posted from the text area.


I also tried this

<cfif isdefined('form.news') and Len(Form.news) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
</cfif>

This still passed over to the database with nothing into the new field.  I can get subject, name to work, but not news.

Am I missing something?
0
 
pinaldaveCommented:
Hi fixx17,
========================================================
page 1
your form
========================================================
page 2
<cfif isdefined('form.news') and Len(Form.news) EQ 0>
<cfset ErrorList = "Please fill out the textbox/textarea">
<cfelse>
your database operation of insert/update
</cfif>
========================================================

Regards,
---Pinal
0
 
fixx17Author Commented:
Pinal,

I've tried it just as you posted.  But for some reason, it still passes through with nothing being inputed into the textarea field.

========================================================
page 1 = postnews.cfm


<cfform name="form" method="post" action="postto1.cfm">
<cfinput type="text" name="name" required="yes" message="Please Enter Your Name">
<cfinput type="text" name="subject" required="yes" message="Please Enter The Subject Of Your News.">
<textarea name="news" cols="60" rows="10"></textarea>
<input type="submit" name="Submit" value="Submit">
========================================================
page 2= postto1.cfm
<cfif isdefined('form.news') and Len(Form.news) EQ 0>
<cfset ErrorList = "Please fill out the textarea">
<cfelse>

<cfquery name="postit" DATASOURCE="news">
  insert into teamnews
  (name,
   subject,
   news )
   values
      ('#form.name#',
       '#form.subject#',
       '#form.news#')

</cfquery>
</cfif>
========================================================


It still does the same thing.  It will pass and nothing gets inputed into the database.  
I just want it to catch if the user forgets to input stuff into the field.

Thanks for tyring.  I will keep plugging away.
0
 
fixx17Author Commented:
Bingo!

This seemed to stop it from inputing with an empty textarea.  Thank you,

BUT now it just shows a blank page.  Is there anyway to setup an alert it tells the user they did not do?

Right now it just shows a blank white page.
0
 
pinaldaveCommented:
Hi fixx17,
<cfif isdefined('form.news') and (Len(Form.news) EQ 0 or form.news eq '')>
<cfset ErrorList = "Please fill out the textarea">
<cfoutput>#ErrorList#<br>Also you can link anything here or type...</cfoutput>
<cfelse>


Regards,
---Pinal
0
 
fixx17Author Commented:
Thanks, I understand more what you did.  

But I think I need to disect this area of the code and understand what is going on.

<cfif isdefined('form.news') and (Len(Form.news) EQ 0 or form.news eq '')>

I will award the points to you.

Thank you.
0
 
pinaldaveCommented:
Hi fixx17,
<cfif isdefined('form.news') -------->>> means if the form.news param is there or not... if it is there... then it is fine... if not then do not proceed


and (Len(Form.news) EQ 0 -------->>> if the lenth is more 0 means it is there... then only....

OROROR

or form.news eq '')-------->>> if it is null...



>


Regards,
---Pinal
0
 
pinaldaveCommented:
Hi fixx17,
it is fun to help as I learn also...
Regards,
---Pinal
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.