Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea Empty check

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?

like image 662
Jhilom Avatar asked Jun 01 '12 11:06

Jhilom


People also ask

How check textarea is empty in PHP?

How do you check if textarea is empty? If by empty you mean it has no contents, then check the user input with strlen() . If your definition of empty includes whitespace only content, do a trim() on the user input first.


1 Answers

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>
like image 159
Jhilom Avatar answered Nov 15 '22 00:11

Jhilom