简体   繁体   中英

Why error message is not displaying

<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?

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM