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