Textarea Empty check
Asked Answered
D

2

6

HTML

<form name="myForm" id="myForm" onsubmit="return validate();" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name">
<textarea name="details" id="details"></textarea>
<input type="submit">
</form>

Javascript

function validate()
{
         if(document.getElementById('details').value == '') 
         {      
        alert("Please Provide Details!");
              document.getElementById('details').focus();
        return false;       
        }
         else if(document.getElementById('name').value == '')
         {      
        alert("Please Provide Name!");
               document.getElementById('name').focus();
        return false;       
        }
         else
           return true;
}

OR

function validate()
    {
             if(document.myForm.details.value == '') 
             {      
            alert("Please Provide Details!");
              document.myForm.details.focus();
            return false;       
            }
             else if(document.myForm.name.value == '')
             {      
            alert("Please Provide Name!");
              document.myForm.name.focus();
            return false;       
            }
             else
               return true;
    }

I have seen the codes from previous Stack Overflow but as I am using these and it is not working. Will anyone help to solve check empty Value on Textarea using Javascript but not jquery.

The Reference I have used how to check the textarea content is blank using javascript? And How to check if a Textarea is empty in Javascript or Jquery?

Deduce answered 1/6, 2012 at 11:29 Comment(2)
It should work FINE!, you must have other errors in you page. Setup a demo at jsfiddle.netDozen
its working fine!!! check if other error exist in your page.Bertie
D
11

I think you may have space problem on your textarea. Use a trim function to reduce that. Here is the example following. I hope it may solve your problem.

JavaScript Add this function

function trimfield(str) 
{ 
    return str.replace(/^\s+|\s+$/g,''); 
}

And your JavaScript function

function validate()
{
     var obj1 = document.getElementById('details');
     var obj2 = document.getElementById('name');
         if(trimfield(obj1.value) == '') 
         {      
              alert("Please Provide Details!");
              obj1.focus();
              return false;       
         }
         else if(trimfield(obj2.value) == '')
         {      
               alert("Please Provide Name!");
               obj2.focus();
               return false;       
        }
         else
           return true;
}

OR

function validate()
    {
         var obj1 = document.myForm.details;
         var obj2 = document.myForm.name;
             if(trimfield(obj1.value) == '') 
             {      
               alert("Please Provide Details!");
               obj1.focus();
               return false;       
             }
             else if(trimfield(obj2.value) == '')
             {      
               alert("Please Provide Name!");
               obj2.focus();
               return false;       
             }
             else
               return true;
    }

And HTML with PHP

<form name="myForm" id="myForm" onsubmit="return validate();" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name">
<textarea name="details" id="details"></textarea>
<input type="submit">
</form>
Deduce answered 4/6, 2012 at 4:21 Comment(0)
Z
3

You can use following jQuery to escape white spaces.

if($("#YourTextAreaID").val().trim().length < 1)
{
    alert("Please Enter Text...");
    return; 
}
Zelma answered 1/2, 2019 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.