0

do i need to add a variable or something? D: help, please?

 <html>
<head>
<script language="javascript" type="text/javascript">
function sub()
{
validateForm();
}
function validateForm()
{
if (document.forms.myForm.fname.value == "" || document.forms.myForm.fname.value == " ")
  {
  alert("First name must be filled out");
  }
if (document.forms.myForm.lname.value == "" || document.forms.myForm.lname.value == " ")
{
alert("Last name must be filled out");
}
else
{
window.location = "ok.html";
}
}
</script>
</head>

<body>
<form name="myForm">
First name: <input type="text" name="fname">
<br>
Last name: <input type="text" name="lname">
<br>
<input type="button" value="Submit" onclick="sub()">
</form>
</body>
</html>

ok so, my code works kind of - i'm not sure how to change that 'else' statement to get it to redirect to the next page if the forms are valid.. do i need to add a variable or...? so basically the code does what i want until you only enter the last name field then it shows the error message but it redirects still -
how do i make it so the code redirects to the success page if both of the fields are valid?

(i don't know much on javascript :/ and i know you can make it alot simplier but we HAVE to do it this way so we force the code to do it or whatever)

2
  • 2
    Please indent your code. Thanks. Commented May 28, 2012 at 18:53
  • 1
    You could add in a few boolean variables that you set true if each condition it's true, and then check with an if statement that both are true and continue from there. That's a little drawn out though. Id give you a proper solution if I wasn't on my phone. Commented May 28, 2012 at 18:57

1 Answer 1

1

This should do the trick:

<html>
    <head>
        <script language="javascript" type="text/javascript">
            function validateForm()
            {
                var worked = true;

                if (document.forms.myForm.fname.value == "" || document.forms.myForm.fname.value == " ")
                {
                    alert("First name must be filled out");
                    worked = false;
                }
                if (document.forms.myForm.lname.value == "" || document.forms.myForm.lname.value == " ")
                {
                    alert("Last name must be filled out");
                    worked = false;
                }

                if (worked)
                {
                    window.location = "ok.html";
                }
            }
        </script>
    </head>

    <body>
        <form name="myForm">
            First name: <input type="text" name="fname">
            <br />
            Last name: <input type="text" name="lname">
            <br />

            <input type="button" value="Submit" onclick="validateForm();">
        </form>
    </body>
</html>

There are numerous ways to do this simple task, and this is how you would do it with a variable. Basically, if any of the checks fail, the variable worked is set to false (to indicate that something didn't work), and only if that variable is left intact (true), can the user proceed to the next step.

PS: ident your code (see how my code is easier to read than yours?). PPS: you definitely shouldn't rely only on JS to validate your forms, you must check it on your servers as well. Someone can easily bypass these checks, as they are only for convenience.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.