Download the following files to your lab4
folder. Remember to Right-click the links and
select Save Link As..
In this part, we will create the following form using very flexible JavaScript functions:
inputs.html
in Notepad++
newForm
, but newForm
has not been defined
newForm
, use the document.querySelector()
method to
select the form element in the body of the web page, and set newForm
to point to that element
functions.js
in Notepad++
makeLabel
function as a model and define a function called makeInput
that takes the following arguments/parameters defined in order:
type
, id
, value
and label
makeInput
function should do the following:
input
elementtype
parameterid
is not defined and not blank, set the id and name attribute to this valuevalue
is not undefined and not blank, set the value attribute to this valuelabel
is not undefined and not blank, do the following:
makeLabel
function to create a new labellabel
is undefined or blank, return the new input elementmakeInput
function as a model and define a function called makeSelect
that takes the following arguments/parameters defined in order:
id
, values
and label
makeSelect
function should do the following:
select
elementid
is not undefined and not blank, set the id and name attribute to this valuefor
loop to iterate over the values
array. For each value in the array do the following:
option
elementinnerHTML
field, use the makeTextNode
function to create
a text node with the array value and then append it to the option element you just created label
is not undefined and not blank, do the following:
makeLabel
function to create a new labellabel
is undefined or blank, return the new select elementmakeArray
the returns an array of strings for a numeric range from start
to end
. For example,
the function call makeArray(2,5)
would return ["2", "3", "4", "5"]
.makeArray
function, create an empty array like this: var myArray = [];
for
loop that goes from x = start
to end
inclusivex
to a string like this: var myString = x.toString()
myArray.push(myString);
inputs.html
in Notepad++ and add the function calls to create all the form elements in the solution.
You should add the following:
return myArray;
Verify that inputs.html
validates at:
html5.validator.nu
Show your instructor that your code matches the solution.
Verify that valid.html
validates at:
html5.validator.nu
Then, try submitting your form with invalid and valid values. Make sure it works.
Assume all the input
elements inside of div
tags are required and cannot be blank.
Rather than write redundant code to check each element, we
can do it with minimal coding.
inputs.html
add an event listener to the form so that a function called checkInputs
is executed when the "submit" event is fired.
functions.js
write the checkInputs
function to do the following:
document.querySelectorAll
to get all the input
elements that are inside of div
elements.
This function returns an array of elements called a nodeList.
Assign the returned array to a variable called inputsArray
for
loop to iterate over the length of the inputsArray
class = "error"
to the element's parentNode
.
Note that this would be the div
surrounding the element.
Also, be sure to call event.preventDefault()
so the form is not submitted.
Remember to pass event
into the function as a parameter. The event
is a global variable
the stores the event that was just fired.
styles.css
that decorates div.error
with red text and a red border.
class = "error"
by calling setAttribute("class", "")
on the element's parentNode
In this part, you will implement validation that displays an error message and disables the "Insert" button. See the solution to understand how the form will function.
Make sure the username is not "admin" or "root"
In inputs.html
add the following code:
document.querySelector("#username").addEventListener("change", function(event) { if (this.value == "root" || this.value == "admin") { this.parentNode.setAttribute("class", "error") document.querySelector("#error_message").innerHTML = "Invalid username"; document.querySelector("#insert_button").disabled = true; event.preventDefault(); } else { this.parentNode.setAttribute("class", "") document.querySelector("#error_message").innerHTML = ""; document.querySelector("#insert_button").disabled = false; } });
Test the code above to make sure it works and then read the following:
this
refers to the selected object or the object being "acted on"this
allows us to change the object inside the anonymous functions without having to
select it again or use a variable.Using part A as a model, make sure the password is 10 or more characters long.
Using part A as a model, make sure the selected "Age" - "Years of Experience" is greater than 18.
Create a zip file of your lab4
folder called lab4.zip
and submit the file in Blackboard. The zip file should contain three files:
inputs.html
, style.css
and functions.js
.
In the comment area of Blackboard put your partner's name.
The deliverable must be submitted in Blackboard by midnight on the day before your next scheduled lab meeting. If you wish to work alone, you can submit the deliverable yourself. However, you are encouraged to work with your partner and you can submit together. When submitting as a lab pair, be sure to include your partner's name in the comment area when you submit via Blackboard.
Outside of lab, you are only permitted to work with your lab partner. Do not share your work with any other student.