javascript onsubmit and 2 functions error -
can please tell me going wrong?
i trying create basic login page , opens when correct password written
<html> <head> <script> function validateform() { var x=document.forms["myform"]["fname"].value; if (x==null || x=="") { alert("first name must filled out"); return false; } var x=document.forms["myform"]["fname2"].value; if (x==null || x=="") { alert("password must filled out"); return false; } } function isvalid(mynorm){ var password = mynorm.value; if (password == "hello_me") { return true; } else {alert('wrong password') return false; } } </script> </head> <body> <form name="myform" action="helloworld.html" onsubmit="return !!(validateform()& isvalid())" method="post"> login id: <input type="text" name="fname"> <br /> <br> password: <input type="password" name="fname2" > <br /> <br /> <br /> <input type="submit" value="submit"> <input type="reset" value="clear"> </form> </body> </html>
password value undefined
in 2nd function because not using params when calling isvalid()
function
var password = mynorm.value; //`mynorm` undefined
to
var password = document.forms["myform"]["fname2"].value;
see below correct function
function isvalid(mynorm){ var password = document.forms["myform"]["fname2"].value; if (password == "hello_me"){ return true; } else { alert('wrong password') return false; } }
updated answer on comment if value valid in function validateform()
return true
, see below
function validateform() { var x=document.forms["myform"]["fname"].value; if (x==null || x=="") { alert("first name must filled out"); return false; } var x=document.forms["myform"]["fname2"].value; if (x==null || x=="") { alert("password must filled out"); return false; } return true; // return true when input value value }
Comments
Post a Comment