简体   繁体   中英

JS Form Validation not working for simple html form

I have a simple HTML form with just one filed. The form is as follows-

<table> 
    <form action="insert.php" method="post" name="discover" onSubmit="return discover()">
    <tr><td></td><td><input type="text"  name="name"></td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>

The validation js file is-

function discover() {
   var stu_name=document.forms["discover"]["name"].value;

   if (stu_name==null || stu_name=="" || stu_name.length<7) {
      alert("Please provide Your full name");
      return false;
   }  
}

I have included the validation js file properly. But the validation is not working. The link for Jsfiddle

try this

<script type="text/javascript">
function check()
{
var stu_name=document.forms["discover"]["stu_name"].value;
if (stu_name==null || stu_name=="" || stu_name.length<7)
  {
  alert("Please provide Your full name");
  return false;
  }
}
</script>
<form action="" method="post" name="discover" id="discover" onsubmit="return check();">
<input type="text" name="stu_name" id="stu_name" value="" />
<input type="submit" id="sub" />
</form>

In your js file,

Test that:

function discover()
{
var stu_name=document.forms["discover"]["name"].value;


if (stu_name==null || stu_name=="" || stu_name.length<7)
{
      alert("Please provide Your full name");
      return false;
   }
}

This work

function discover()
{
var stu_name=document.getElementsByName("name");


if (stu_name==null || stu_name=="" || stu_name.length<7)
{
      alert("Please provide Your full name");
      return false;
   }
}

You are mixing form name and function name and also the input needs an id which was missing. Also you forgot to close the function. Try this

<script type="text/javascript">
function _discover()
{
var stu_name=document.forms["discover"]["stu_name"].value;


if (stu_name==null || stu_name=="" || stu_name.length<7)
  {
  alert("Please provide Your full name");
  return false;
  }
}


</script>

<table> 
    <form action="" method="post" name="discover" onSubmit="return _discover();">
    <tr><td></td><td><input type="text"  name="name" id="stu_name"> </td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>

There is Nothing Wrong in your Code except the function name as same name as form

see this link here's the working code

jsbin

function disc() {
   var stu_name=document.forms["discover"]["name"].value;

   if (stu_name==null || stu_name=="" || stu_name.length<7) {
      alert("Please provide Your full name");
      return false;
   }  
}



<table> 
    <form action="insert.php" method="post" name="discover" onSubmit="return disc()">
    <tr><td></td><td><input type="text"  name="name"></td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>

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