0
<html>
    <head>
        <script type="text/javascript"> 
        function validate() { 
            var a = document.form.name.value;

            if(!(a)) { 
                document.getElementById("errorBox").innerHTML = "*Please fill required"; 
            } else { 
                document.getElementById("errorBox").innerHTML = ""; 
            }
        } 
        </script> 
    </head> 
    <body> 
        <form method= "post" name="form" onsubmit="return validate();"> 
            Name :<input type="text" id="name" name="name" />
            <div id="errorBox"> </div> <br> 
            <input type="submit" name="submit" value="submit" /> 
            <input type="Reset" name="Reset" /> 
    </body>

Don't know why that error message is not showing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
prathap
  • 31
  • 6

2 Answers2

1

In javascript, an empty string is not the same as a null value or an undefined... but they all resolve to false in a condition !

Try this:

 if(!a){
     // your code
 }

Have a look at this fiddle for a working solution: https://jsfiddle.net/wvofbjdk/

If it does not work in your code, then the error is elsewhere. Have a look at the console for errors (see this link to get started on chrome: https://developers.google.com/web/tools/chrome-devtools/debug/console/?hl=en).

Derlin
  • 9,572
  • 2
  • 32
  • 53
1

Replace your following code

var a = document.form.name.value;
if( a == "" )
        {
    document.getElementById("errorBox").innerHTML = "*Please fill required";

  } 

with this code

var a = document.getElementById("name").value;
if(!(a))
{
    document.getElementById("errorBox").innerHTML = "*Please fill required";

}

If still issue not solved, kindly show us ur html code so that we check is there any error or not.

Imran Qamer
  • 2,253
  • 3
  • 29
  • 52
  • Name :

    – prathap Aug 18 '16 at 06:35
  • Nothing happens Imran ...Dono how to post my complete code thatswhy posted in comment area – prathap Aug 18 '16 at 06:36
  • u welcome, if it works for u. always remember to accept answer for future users, by clicking on tick symbol. – Imran Qamer Aug 18 '16 at 11:05