Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:08:42+00:00 2026-06-04T15:08:42+00:00

do i need to add a variable or something? D: help, please? <html> <head>

  • 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:08:43+00:00Added an answer on June 4, 2026 at 3:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm receiving string variable in php and need to add something to it. I
I need to add append data to a string if a certain variable is
I am have a column of dates that I need to add a variable
$_SESSION[{'\'testingrecord'.$testingrecordsfec.'\''}] this originally looked like this ${'testingrecord'.$testingrecordsfec} but I need to add the variables
I need add a new user group for mediawiki. The new group has more
need to add a 2nd css stylesheet to a page. do i add a
I need to add an invite users functionality to my social networking application so
I need to add CRRedist2008_x86.msi to my deployment project as it is a requirement
I need to add css height to each page dynamically with jQuery. So far
I need to add a new column to a MS SQL 2005 database with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.