0

why this code is not working ?

<!DOCTYPE html> <html> <head> <script> function validateForm() { var x = document.forms["myForm"]["fname"]["lname"].value; if (x == "") { alert("Name must be filled out"); return false; } } </script> </head> <body> <form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="fname"> <br> Last name:<input type="text" name="lname"> <input type="submit" value="Submit"> </form> </body> </html>

13th Jul 2018, 1:29 PM
Nayem
2 Answers
+ 3
Or var x = myForm[0].value; var y = myForm[1].value; if (x == "" || y == "") { // .... return false; } https://code.sololearn.com/W6BXbUI3v7mJ/?ref=app
13th Jul 2018, 1:55 PM
CalviŐČ
CalviŐČ - avatar
0
You can't get both fname and lname at the same time. You should do something like this... var x = document.forms["myForm"]["fname"].value; var y = document.forms["myForm"]["lname"].value if (x == "" || y == "") { //you can finish this bit }
13th Jul 2018, 1:36 PM
James
James - avatar