html - Javascript form validation not working for 2 input fields -


i trying validate form using javascript. want if user leaves input field empty see alert , form not submit data php file. unfortunatlly, code below not working me. javascript not executing on form submit.

javascript

 <script>  function myform() {  var x =document.forms["myform"]["name"].value;   if (x == null || x == "")     {alert("name must filled out");     return false;    }   { var y=document.forms["myform"]["age"].value;  if (y == null || y == "") {     alert("age must filled out");     return false;   }  } 

form

 <form name="myform"action="game.php"onsubmit="return myform()"method="post">      name: <input type="text" name="name">   age: <input type="text" name="age">  <input type="submit" value="submit">  </form> 

any appriciated.

you've got couple typos in code. way find them use console in developer tools of browser (try pressing f12).

change

{ var y=document.forms["myform"]["age"].value; 

to

var y=document.forms["myform"]["age"].value; //removed opening curly brace 

and in form change:

onsubmit="return myform()" 

to

onsubmit="return myform()" 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -