Solved

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

Posted on 2004-10-22
221 Views
Last Modified: 2013-12-24
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?
0
Question by:fixx17
    15 Comments
     
    LVL 35

    Expert Comment

    by:mrichmon
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 1

    Author Comment

    by:fixx17
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 1

    Author Comment

    by:fixx17
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 1

    Author Comment

    by:fixx17
    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
     
    LVL 22

    Accepted Solution

    by:
    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
     
    LVL 1

    Author Comment

    by:fixx17
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 1

    Author Comment

    by:fixx17
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    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
     
    LVL 22

    Expert Comment

    by:pinaldave
    Hi fixx17,
    it is fun to help as I learn also...
    Regards,
    ---Pinal
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
    Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
    The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
    The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now