Question

Using JavaScript, I want to make a function that will open a new alert box that will say hello fi...

Using JavaScript, I want to make a function that will open a new alert box that will say hello first name, last name after the person logs into the sign in form. This will happen aftrer the validation function is invoked. See code below. See the welcomefunction().

<!DOCTYPE html>
<html>
<head>
<script> //validate the login form
function validateForm() {
var x = document.forms["LoginForm"]["fname"].value; //set x and y to first name and last name
var y = document.forms["LoginForm"]["lname"].value;
if (x == "" || y == "") {
alert("First and last name must be filled out."); //send alert you must complete first and last name
return false;
}
}
</script>

<!--
<script>
function welcomeFunction() {
var x = document.forms["LoginForm"]["fname"].value; //set x and y to first name and last name
var y = document.forms["LoginForm"]["lname"].value;
if (x || y != null) {
document.getElementById("demo").innerHTML =
"Hello " + " x " + " y "! How are you today?";
}
}
</script>

-->


</head>
<body>
<form name="LoginForm" action="/action_page.php" onsubmit="return validateForm()" method="post">
First name:<br>
<input type="text" name="fname" value="">
<br>
Last name:<br>
<input type="text" name="lname" value="">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//from what I infer you want the required alert box to pop up before the page redirection. If not make a followup comment, I'll modify it accordingly.

//moved the welcomeFunction() to above the validateForm() and clubbed the two Script tags, there's no need for multiple script tags

//I only had to make minor modifications to the code, you were almost there.

//replace both of your script tag with the following single script tag.

<script> //validate the login form

function welcomeFunction() {

    var x = document.forms["LoginForm"]["fname"].value; //set x and y to first name and last name

    var y = document.forms["LoginForm"]["lname"].value;

    if (x || y != null)

    {

        //added the following line fo code to open an alert box

        alert("Hello " + x + y +"! How are you today?");

        //i believe there was a typo in the followng line too, removed quotes from around x and y

        document.getElementById("demo").innerHTML =

        "Hello " + x + y +"! How are you today?";

    }

}

function validateForm() {

    var x = document.forms["LoginForm"]["fname"].value; //set x and y to first name and last name

    var y = document.forms["LoginForm"]["lname"].value;

    if (x == "" || y == "")

    {

        alert("First and last name must be filled out."); //send alert you must complete first and last name

        return false;

    }

    else

    //if fields are filled out welcomeFunction() will be executed

    {

        welcomeFunction()

    }

}

</script>

Add a comment
Know the answer?
Add Answer to:
Using JavaScript, I want to make a function that will open a new alert box that will say hello fi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify this code to store the form elements as variables and display them on the page...

    Modify this code to store the form elements as variables and display them on the page in an HTML table. <!DOCTYPE html> <html> <head> <script> function getValues() {     var result = ""; result += "First Name: " + document.forms["myForm"]["fname"].value + "<br>"; result += "Last Name: " + document.forms["myForm"]["lname"].value + "<br>"; result += "Address: " + document.forms["myForm"]["address"].value + "<br>"; result += "City: " + document.forms["myForm"]["city"].value + "<br>"; result += "State: " + document.forms["myForm"]["state"].value + "<br>"; document.getElementById("output").innerHTML = result; } </script>...

  • Using Javascript, I have two radio buttons, depending on the value of the radio button 1...

    Using Javascript, I have two radio buttons, depending on the value of the radio button 1 or 2, you will send a different form to the user. So, if radio button is value "1" send new form1 or if value "2" send new form 2.  New form will ask for letter grades for four courses in textboxes. Onsubmit of new form letter grades A,B,C,D need to be changed to numeric values 1,2,3,4 so an average grade for the four couses can...

  • <html>     <head>       <title>Sign Up page</title>   <form name="validationForm" method="post" onsubmit="return checkvalidation()">      &n

    <html>     <head>       <title>Sign Up page</title>   <form name="validationForm" method="post" onsubmit="return checkvalidation()">         <!--div class-->       <div class="formvalidation">       <label>Your first Name</label>        <span id="showname"></span>        <!--label for firstname-->       <input type="text" name="firstname" class="formsignup"  id ="firstn" placeholder="Enter your Name">      <br><br>           <!--lastname-->       <label>Your last Name</label> <span id="showlname"></span>       <input type="text" name="lastname" class="formsignup" id="lastn" placeholder="Enter your last Name">       <br><br>        <!--email-->         <label>Your Email</label>          <span id="showemail"></span>         <input type="email" name="emailid" class="formsignup" size="45" id="emailn" placeholder="Enter your Email">        <br><br> <input type="submit" value="send">     </div>           </form> <script>      function checkvalidation(){     var name = document.forms["validationForm"]["firstname"].value;     var lname...

  • Modify the following file, so that: If the user input for the age input box is...

    Modify the following file, so that: If the user input for the age input box is empty, please send the user a warning message: “Age field cannot be empty” and return to the form web page to allow the user to re-input the age; If the user input for the age input box is incorrect (e. g. less than 1), please also send the user a warning message: “Your age input is not correct!” and return to the form web...

  • i'm having trouble with 1 and 3 using html, javascript and jquery 1) Change the "Search"...

    i'm having trouble with 1 and 3 using html, javascript and jquery 1) Change the "Search" link on the left to a button that when clicked, hide all the fields on the form and display the text "This is a search feature..." 3) When the user fills the form and clicks "Login", the values inputted should be displayed in the empty row (under Login) of the page. The display should be in the form: Student ID: <input value> Student Name:...

  • Add comments to it by filling in the blank comments numbers 1 through 32. (The first...

    Add comments to it by filling in the blank comments numbers 1 through 32. (The first few comments are HTML comments, not JavaScript comments, and are bracketed according to <!- this is an HTML comment>.) <!DOCTYPE html> <html> <body onload="handleFormAction()"> <h2>Form handling example in JavaScript</h2> <h3>(Or, John Doe meets Robbie Robot)</h3> <h4>Instructions: Fill in the form. Then fill in comments 1 through 23 in the JavaScript code!</h4> <form target="_blank" action= > <!- 1: replace this placeholder HTML comment with a...

  • <!DOCTYPE html> <html> <head> <!-- JavaScript 6th Edition Chapter 8 Hands-on Project 8-1 Author: Date:   ...

    <!DOCTYPE html> <html> <head> <!-- JavaScript 6th Edition Chapter 8 Hands-on Project 8-1 Author: Date:    Filename: index.htm --> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>Hands-on Project 8-1</title> <link rel="stylesheet" href="styles.css" /> <script src="modernizr.custom.65897.js"></script> </head> <body> <header> <h1> Hands-on Project 8-1 </h1> </header> <article> <h2>New Account Information</h2> <form> <fieldset id="deliveryinfo"> <label for="fnameinput">First Name</label> <input type="text" id="fnameinput" name="fname" /> <label for="lnameinput">Last Name</label> <input type="text" id="lnameinput" name="lname" /> <label for="addrinput">Street Address</label> <input type="text" id="addrinput" name="address" /> <label for="cityinput">City</label> <input type="text" id="cityinput" name="city"...

  • I need to complete 3 validation cases in this Javascript code. I need to validate email,...

    I need to complete 3 validation cases in this Javascript code. I need to validate email, phone and street address. How would I go about validating these 3 cases in this code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Week 10: Regular Expressions</title> <style type="text/css"> span { padding: 2px; } .success { color: #008000; background: #99cc99; } .failure { color: #ff0000; background: #ff9999; } </style> <script type="text/javascript"> function validateInput(form) { var result0 = document.getElementById('result0'), result1 = document.getElementById('result1'), result2 = document.getElementById('result2'),...

  • LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but,...

    LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but, I keep getting an error that I coded "Wrong username and password!" ***PLEASE DONT GIVE ME A NEW CODE THAT IS NOWHERE NEAR THE CODE I SUBMITTED***** PLEASE LOOK AT THE CODE AND SEE ANY ISSUES login.html <!DOCTYPE html> <html> <head> <title>login popup</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> body { background-color: white; } </style> </head> <body> <center> <h1 style="text-align: center;"> Login </h1> <form>             Login ID:...

  • Unit 1 Programming Assignment Follow the directions for each of the following questions. Answer the question...

    Unit 1 Programming Assignment Follow the directions for each of the following questions. Answer the question or provide the code in a space below the question. 1. Write the complete script tag set for a script whose line statement is document.write(“Hello, world.”); 2. Build a complete HTML document and include the answer to the previous question such that the page executes the script as the page loads. Open the document in your browser to test the results. 3. Add a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT