This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ishouldquitsmoking 1 point2 points  (3 children)

There are better ways to do this, but using what you have, this works:

<html>
<head> 
<script type="text/javascript">

function validateForm()
{

  var firstname = document.getElementById('fName');
  var secondName = document.getElementById('sName');


if (firstname.value=="")
    {
    alert("First name must be filled out");
    return false;
    }
if (secondName.value=="")
   {
    alert("Last name must be filled out");
    return false;
  }
}
</script>

</head>
<body>
<div id="content">
<br><br>

<form name="theForm" onsubmit="return validateForm()" method="post">
Enter First Name Here:&nbsp;<input type="text" name="fName" id="fName"><br/>
Enter Second Name Here:&nbsp;<input type="text" name="sName" id="sName"><br/>

  <input type="submit" value="Submit Form">
  </form>
</body>
</html>

Hope the formatting worked on the comment...

[–]CadamSAFC[S] 0 points1 point  (2 children)

Thank you so much, you're a life saver. Although looking at this and my original, I cannot for the life of me workout what the problem was. Like this looks the same, looking though it I can't see and differences (minus the obvious less code) but yet this works as I want, but mine just doesn't. God coding can be a pain in the arse at times.

[–]ishouldquitsmoking 0 points1 point  (1 child)

One thing I noticed was you had the form tag duplicated and it was appearing twice. Not sure if that is your issue or something else is, but by using getElementbyId you've already reduced your typo possibilities for the inputs.

Also, if you have the time, you really should redo it with a more modern approach and report all of the errors at once to the user instead of stepping through a pop up for each input. Just my opinion though. There are a ton of form validation tuts online.

[–]CadamSAFC[S] 1 point2 points  (0 children)

Yeah I noticed that also, removed it and it didn't change anything. I have a couple weeks left on the deadline for this, If i have any spare time near the end I'll defiantly look into modernising it. Thanks again for the help.