Can anyone tell why alert box is not showing up? | Sololearn: Learn to code for FREE!
+ 1

Can anyone tell why alert box is not showing up?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Javascript Example</title> <link rel="stylesheet" href="style.css"> </head> <body> <form method="post" action="something.php" name="my Form" onsubmit="return validateForm()"> <div> <label>First Name</label> <input type="text" name="fname" id="firstname"> </div> <div> <label for="">Last Name</label> <input type="text" name="lastname" id="lastname"> </div> <input type="submit" value="submit"/> </form> </div> <script> function validateForm(){ var x = document.forms["myForm"]["fname"].value; if(x == ""){ alert("first name is required"); return false; } } </script> </body> </html>

26th Aug 2017, 1:18 PM
Neeraj Kumar
Neeraj Kumar - avatar
2 odpowiedzi
+ 7
erm...... see the form name there's a extra white space....
26th Aug 2017, 2:11 PM
Leon lit
Leon lit - avatar
+ 3
@Leon lit wrote: << erm...... see the form name there's a extra white space.... >> Or a missing one in the index name of document.forms object in JS: attribute 'name' could contains any string, and is not limited to only 'safe' character as used for variables names (or 'id' attribute) ;)
26th Aug 2017, 3:34 PM
visph
visph - avatar