Question

Form Processing HTML

One of the most ubiquitous uses of JavaScript is validating form data on the client side before it is submitted to the server. It is done everywhere because it is fast and it gives you a great deal of flexibility in how you handle errors insofar as the GUI is concerned.

Attached is an image of some code I wrote (so Blackboard can't mess it up). Some things to notice that will help you with the lab.

When you write the input fields, you want to put in an id and a name attribute. JavaScript likes to work with ids. But PHP wants get the data you submit to the server using the name attribute. To save yourself some confusion, make these the same.

Each field has a label associated with it. You want to turn that label red if there is a problem. To do this, you can put some CSS at the beginning of your head section. Write two classes, one for redtext and one for blacktext. Then if there is an error, switch the label's class to the redtext class. This is why we put an id field on the label, so we can get a hold of it and manipulate the class.

At the top of the form you can put some h2 tags and between them put a span tag with an id. Then use that id to get a hold of that area and change the innerHTML to the error message if they leave a field blank.

Input fields have an attribute called disabled. You can set that via JavaScript. If the marital status field has the value Married, then the Spouse Name field needs the disabled attribute set to false, i.e. it needs to be active so it should NOT be disabled.

You will need to get the values for password and password2 and compare them. If they are not equal, change the label class to redtext and display the error message

Use a table to align and organize your fields.

You are going to submit the form to a PHP script I wrote that will display the entered values. Submit the form to the URL given below. If you name the fields correctly all the data should be displayed on the resultant page.
[form action="http://www.waynedog.net/formreader.php” method=”post” ]

Write an event handler to validate the fields when the SUBMIT button is clicked.

If all the fields are complete, submit the form to the URL above.

All fields are required. If a field is not filled in, do not submit the data, instead set the field's label to red and at the top of the form put a message in red that says:
Please correct the following errors:

If the two password fields do not match, set the label to red and display the error message.

Write an event handler for the RESET button to clear and reset all values on the form.

In the onload function set the focus to the first name field.

The Marital Status field - Options for the Select are: Single Married

Write an event handler for onchange that disables the Spouse Name field if they pick Single. The Spouse Name should be active if they pick Married.

Include these lines at the beginning of your JavaScript and use the $ function to get the elements by id.

"use strict";
var $ = function(id) { return document.getElementById(id); };

NOTE: In the book they have an example of an HTML form and they use the Node interface. For this assignment use the DOM interface and the DOC HTML Specification.

Below is the label, the type of input, and the name and id to give the field. You will want to use the id in your JavaScript. The name is required for the PHP processing script I wrote that you use for the submit.

To save you some typing I have attached a file with the states listed in the format you need for the select option.

Registration Form Format is: Label - type - name/id

First Name - text - fname

Last Name - text - lname

Address - text - address1

Suite or Apt. - text - address2

City - text - city

State - select - state

ZIP Code - text - zipcode

Gender - radio button - gender

Birthdate - date - dob

Marital Status - drop down - mstatus

Spouse Name - text - sname

Email - email - email

Phone - text - phone

Password - password - password

Confirm Password - password - password2

RESET button

SUBMIT button

For example, based on this, your code for the first name field should look like this:

[label id="fnameLabel"]First name[/label][input type="text" id="fname" name="fname"]

Here is some pseudo-code that lays out how to approach lab 4, I have also included some JavaScript code in some places to give you an idea of how to approach that section.

onload section {

set up event for maritalStatus drop down
$('mstatus').onchange = mstatusChange;

set up event for Submit button
$('submit').onclick = processForm;

set focus to fname field

}

function mstatusChange() {

get the value from mstatus drop down

disable or enable spouse name based on value

if (mstatus == 'Married')
$('sname')..disabled = false;
else
$('sname')..disabled = true;

function processForm() {

get all the form values

var lname = $('lname').value;
var fname = $('fname').value;
repeat for each field on form

Set a flag saying everything entered is correct
var formGood = true;

Check the fields on the form
if a field fails, then
set the formGood flag to false
set the class for the label to red
else
set the class for the label to black (just n case it had been set to red earlier)

if (lname == "") {
  formGood = false;
$('lnameLabel').className = 'redtext';
} else {
$('lnameLabel').className = 'blacktext';
}

check that the passwords are the same

if (password != password2) {
  formGood = false;
$(passwordLabel).className = 'redtext';
set the password2 label red also
} else {
set the label classes to blacktext
}

if formGood is still true then
submit the form
else
display error message

if (formGood) {
$("myForm").submit();
} else {
$('errorMsg').innerHTML = "Please fix the errors below";
}

}

------

The code above assumes a few things you need in the HTML

In the head you will need to define the CSS classes used for the label colors. Nothing fancy

<style>
.blacktext { color: black; }
.redtext { color: red; }
</style>

Also at the top of the HTML you need a place for the error message with an ID you can get a hold of. Might as well make it red while we are at it since we have the class already.

[h2 id='errorMsg' class='redtext'][/h2]

Finally in the form tag we need to include an ID that we can use when we submit the form.

form id="myForm" action="http://www.waynedog.net/formreader.php" method="post"

Attached Files

state-select-options.txt

 <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
  <option value="CO">Colorado</option>
  <option value="CT">Connecticut</option>
  <option value="DE">Delaware</option>
  <option value="DC">Dist of Columbia</option>
  <option value="FL">Florida</option>
  <option value="GA">Georgia</option>
  <option value="HI">Hawaii</option>
  <option value="ID">Idaho</option>
  <option value="IL">Illinois</option>
  <option value="IN">Indiana</option>
  <option value="IA">Iowa</option>
  <option value="KS">Kansas</option>
  <option value="KY">Kentucky</option>
  <option value="LA">Louisiana</option>
  <option value="ME">Maine</option>
  <option value="MD">Maryland</option>
  <option value="MA">Massachusetts</option>
  <option value="MI">Michigan</option>
  <option value="MN">Minnesota</option>
  <option value="MS">Mississippi</option>
  <option value="MO">Missouri</option>
  <option value="MT">Montana</option>
  <option value="NE">Nebraska</option>
  <option value="NV">Nevada</option>
  <option value="NH">New Hampshire</option>
  <option value="NJ">New Jersey</option>
  <option value="NM">New Mexico</option>
  <option value="NY">New York</option>
  <option value="NC">North Carolina</option>
  <option value="ND">North Dakota</option>
  <option value="OH">Ohio</option>
  <option value="OK">Oklahoma</option>
  <option value="OR">Oregon</option>
  <option value="PA">Pennsylvania</option>
  <option value="RI">Rhode Island</option>
  <option value="SC">South Carolina</option>
  <option value="SD">South Dakota</option>
  <option value="TN">Tennessee</option>
  <option value="TX">Texas</option>
  <option value="UT">Utah</option>
  <option value="VT">Vermont</option>
  <option value="VA">Virginia</option>
  <option value="WA">Washington</option>
  <option value="WV">West Virginia</option>
  <option value="WI">Wisconsin</option>
  <option value="WY">Wyoming</option>

png file

<head> <style> .blacktext { color:black; } .redtext {color:red; } </style> <script> if (see if value of mstatus==Married) {

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

Working code implemented in HTML & JS and appropriate comments provided for better understanding:

Source code for index.html:

<html>
<head>
<title>Lab</title>
<style>
.blacktext {color:black;}
.redtext {color:red;}
</style>
   <script type="text/javascript">
//set focus to first name box on load
window.onload = function() {
document.getElementById('fname').focus
           //event for maritialStatus drop down
           $('mstatus').onchange=mstatusChange;
       };
       function $(id) {
       return document.getElementById(id);
       }
//event for submit button
//$('submit').onclick=processForm;

function mstatusChange() {
       var mstatus = $('mstatus').value;
if(mstatus=='Married')
$('sname').disabled=false;
else
$('sname').disabled=true;
}
  
      
function processForm() {
       //alert("HEY");
      
var lname=$('lname').value;
var fname=$('fname').value;
var address1=$('address1').value;
var address2=$('address2').value;
var city=$('city').value;
var zipcode=$('zipcode').value;
var state=$('state').value;
var gender=$('gender').value;
var dob=$('dob').value;
var mstatus=$('mstatus').value;
var sname=$('sname').value;
var email=$('email').value;
var phone=$('phone').value;
var password=$('password').value;
var password2=$('password2').value;

var formGood=true;
if(fname=="") {
formGood=false;
$('fnameLabel').className='redtext';
} else {
$('fnameLabel').className='blacktext';
}

if(lname=="") {
formGood=false;
$('lnameLabel').className='redtext';
} else {
$('lnameLabel').className='blacktext';
}

if(address1=="") {
formGood=false;
$('address1Label').className='redtext';
} else {
$('address1Label').className='blacktext';
}

if(city=="") {
formGood=false;
$('cityLabel').className='redtext';
} else {
$('cityLabel').className='blacktext';
}

if(state=="") {
formGood=false;
$('stateLabel').className='redtext';
} else {
$('stateLabel').className='blacktext';
}

if(zipcode=="") {
formGood=false;
$('zipcodeLabel').className='redtext';
} else {
$('zipcodeLabel').className='blacktext';
}

if(dob=="") {
formGood=false;
$('dobLabel').className='redtext';
} else {
$('dobLabel').className='blacktext';
}

if(mstatus=="") {
formGood=false;
$('mstatusLabel').className='redtext';
} else {
$('mstatusLabel').className='blacktext';
}

if(email=="") {
formGood=false;
$('emailLabel').className='redtext';
} else {
$('emailLabel').className='blacktext';
}

if(phone=="") {
formGood=false;
$('phoneLabel').className='redtext';
} else {
$('phoneLabel').className='blacktext';
}

if(password=="") {
formGood=false;
$('passwordLabel').className='redtext';
} else {
$('passwordLabel').className='blacktext';
}

if(password2=="") {
formGood=false;
$('password2Label').className='redtext';
} else {
$('password2Label').className='blacktext';
}

if(password!=password2) {
formGood=false;
$('passwordLabel').className='redtext';
} else {
$('passwordLabel').className='blacktext';
}

if(formGood) {
           alert("SUBMIT");
$("myForm").submit();
} else {
                       alert("NO SUBMIT");

$('errorMsg').innerHTML="Please fix the errors below";
}
}
   </script>
</head>
<body>
   <main>
<h2 id='errorMsg' class='redtext'></h2>

<h2>Registration Form</h2>

<form id="myForm" action="#ReplaceYourURL" method="post">
<!---<form id="myForm">--->
       <table>
<tr>
<td><label id="fnameLabel">First Name:</label><input type="text" id="fname" name="fname"></td>
       <td><label id="lnameLabel">Last Name:</label><input type="text" id="lname" name="lname"></td>
</tr>
<tr>
<td><label id="address1Label">Address:</label><input type="text" id="address1" name="address1"></td>
       <td><label id="address2Label">Suite or Apt.:</label><input type="text" id="address2" name="address2"></td>
</tr>
<tr>
<td><label id="cityLabel">City:</label><input type="text" id="city" name="city"></td>
<td><label id="stateLabel">State:</label>
<select id="state" name="state">
<option>Please select</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">Dist of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select></td>
</tr>
<tr>
<td><label id="zipcodeLabel">Zip Code:</label><input type="text" id="zipcode" name="zipcode"></td>
</tr>
<tr>
<td>
<input type="radio" id="genderLabel" class="blacktext" name="gender">
<label id="gender">Male</label><br>
<input type="radio" id="genderLabel" class="blacktext" name="gender">
<label id="gender">Female</label><br>
<input type="radio" id="genderLabel" class="blacktext" name="gender">
<label id="gender">Other</label>
</td>
<td><label id="dobLabel">Birthdate:</label><input type="date" id="dob" name="dob"></td>
</tr>
<tr>
<td>
<label id="mstatusLabel" for="mstatusLabel">Marital Status:</label>
<select id="mstatus"name="mstatus">
<option>Please select</option>
<option value="single">Single</option>
<option value="Married">Married</option>
</select>
</td>
<td><label id="snameLabel">Spouse Name:</label><input type="text" id="sname" name="sname"></td>
</tr>
<tr>
<td><label id="emailLabel">Email:</label><input type="email" id="email" name="email"></td>
       <td><label id="phoneLabel">Phone:</label><input type="text" id="phone" name="phone"></td>
</tr>
<tr>
<td><label id="passwordLabel">Password:</label><input type="password" id="password" name="password"></td>
       <td><label id="password2Label">Confirm Password:</label><input type="password" id="password2" name="password2"></td>
</tr>
<tr>
<td><input type="button" value="Submit" onclick="processForm()"></td>
<td><input type="button" value="Reset" onClick="this.form.reset()"></td>
</tr>
</table>
</form>

<h2></h2>
   </main>
</body>
</html>

Output Screenshots:

Lab X + 0 File A:/CheggS/58/index.html Please fix the errors below Registration Form Last Name: Smith Suite or Apt.: Building

Hope it helps, if you like the answer give it a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on...
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
  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • PHP code that is given : <?php // Here is where your preprocessing code goes //...

    PHP code that is given : <?php // Here is where your preprocessing code goes // An example is already given to you for the First Name $fname = $_GET['fname']; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Exercise 2 - GET Echo</title> <style> body { margin:0; padding:0; font-family: Arial; } form { margin:20px; } input[type="text"], input[type="password"] { width:150px; padding:3px; font-size:1em; } input[type="submit"] { padding:3px; font-size:1em; } label { display:inline-block; width:150px; } .input-container { padding:5px; } </style> </head> <body> <form...

  • <!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"...

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

  • Please help with this assignment section A. This question is from world wide web Info Sys Dev. Section A te HTML/JavaSc...

    Please help with this assignment section A. This question is from world wide web Info Sys Dev. Section A te HTML/JavaScript web page called sectiona.html, that has the fpllowing items: a. 2 text items labeled as "CAD Dollar" and "US Dollar", the first(CAD dollars) is enabled with id-"cad", while the later is disabled with id-us" b.1 button named as "convert". functionality: When the user clicks on the button, a JavaScript function named "convert" will be called to convert the value...

  • Both codes <!DOCTYPE html> <html> <head> <title>Week 8 Lab - JavaScript DOM and Arrays</title> <meta charset="utf-8">...

    Both codes <!DOCTYPE html> <html> <head> <title>Week 8 Lab - JavaScript DOM and Arrays</title> <meta charset="utf-8"> </head> <body> <h2>Order Form</h2> <form name="orderForm" method="post" action="processForm.html"> <table> <tr> <th colspan="2">Personal Information</th> </tr> <tr> <td>First Name:</td> <td><input type="text" name="firstName" id="firstName" size="30"></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lastName" id="lastName" size="30"></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="address" id="address" size="30"></td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" id="city" size="30"></td> </tr> <tr> <td>Province:</td> <td><select name="province" id="province" size="1"> <option disabled>Select a province</option> <option value="BC">British Columbia</option> <option value="AB">Alberta</option> <option...

  • given below are the project description and their Html and css files i need javascript according to the project and other files!

    HTML------------------------------------------------------CSS---------------------------------------------------WEB230 - JavaScript 1 Assignment 7 - FormsSome of these tasks would be better done in HTML or CSS but do them in JavaScript to practice whatwe have learned.1. Select the form element and save it in a variable. From here we can access all of the form fields.2. When the page loads do the following:add the password value "monkey"select the favourite city "New York"clear the textarea3. Add an event handler to the "name" field to change the background color...

  • 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 ==...

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

  • I need help writting a Javascript function that does the following its for a HTML/CSS Shopping...

    I need help writting a Javascript function that does the following its for a HTML/CSS Shopping Cart Page CODE This page contains a list of the selected products that includes an image, name, price, quantity, and cost) Because the process is implemented not as real, three products along with its image and price are displayed on the page. The product ID of each product is stored in a hidden textbox. The default amount of each product is 1 and the...

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