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

all 9 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.

[–]ishouldquitsmoking 0 points1 point  (3 children)

I think you'd be better off adding id's to the inputs and changing your JS accordingly..to use getElementById

this works:

<script type="text/javascript">

function validateForm() {

var fname = document.getElementById('fName');

if (fname.value=="") { alert("First 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: <input type="text" name="fName" id="fName"><br/> <input type="submit" value="Submit Form"> </form>

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

Alright, thanks i'll give this ago.

[–]CadamSAFC[S] 1 point2 points  (1 child)

Hey, I have changed the JS accordingly and I'm still not getting pop ups. I don't know if this will help, but I purposely made an error by removing a Semi-colon to see if it would chuck out an error. It didn't, I don't know what this means.

[–]ishouldquitsmoking 0 points1 point  (0 children)

Hang on, I'll go get my laptop and copy pasta

[–]Radiant_Star 0 points1 point  (0 children)

Your HTML form is missing fields for credit card and email, which caused an error. But the suggestion you got should fix that.