Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Empty textarea

Status
Not open for further replies.

LakshmiKiran

Programmer
Jul 1, 2003
22
0
0
IN
How do I check whether a textarea is empty? If the user does not enter any data but presses the enter key, then the form.textarea.value is not empty. How to keep a check on this?
 
Hi LakshmiKiran, what you need a simple JavaScript validation code. You can use validation to check if something is entered before submission, or if the correct information is entered.
This sample will do what you want:

Put this between the <head></head> section:
Code:
<script>
function checkData() {
  var txtCheck = document.formName;
    if(txtCheck.textarea.value == &quot;&quot;)  {
      alert(&quot;Please enter information in the textarea&quot;);
      txtCheck.textarea.focus();
      return false;
    }
}
</script>



Put this after the <body> section:
Code:
<form name=&quot;formName&quot; method=&quot;post&quot; action=&quot;&quot; onSubmit=&quot;return checkData();&quot;>   
    <textarea name=&quot;textarea&quot;></textarea>
  </p>  <p>
    <input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;Submit&quot;>
  </p>
</form>


I have this little thing, Advanced Delusionary Schizophrenia with Involuntary Narcissistic Rage.
It's no big deal really...
 
I checked this. This does not work for textareas. Press Enter without typing any text and the system accepts.
 
howsabout this:
Code:
<script>
function checkData() {
  var txtCheck = document.formName;
    if(txtCheck.textarea.value == 0)  {
      alert(&quot;Please enter information in the textarea&quot;);
      txtCheck.textarea.focus();
      return false;
    }
}
</script>

Known is handfull, Unknown is worldfull
 
LakshmiKiran, I tested the code before I posted, like I do with all my posts. Did you copy/paste the code like I posted avbove??
Did you make sure you were calling the right function name?? The right textarea name?? The right form name?? I used a generic form name and a generic textarea name, if your is different then in the function, you have to change the names apporpriately.

I have this little thing, Advanced Delusionary Schizophrenia with Involuntary Narcissistic Rage.
It's no big deal really...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top