Homework 2
Homework 2
INSTRUCTION:
STUDENT IDENTIFICATION:
Name: ………………………………….. ID: …………………… Signature: …………………………
Homework 2
(jQuery)
This assignment is designed to check your understanding of jQuery syntax and programming structures.
Make sure to read the related chapters covered. You also need to go over the in-labs we did in class
before attempting this assignment.
- read the text value of a text input in a form having the id="username"
var inputValue = $("#username").val();
//equivalent to the javascript instruction
var inputValue = document.getElementById("username").value;
The same functions val() and text() could be used to set a text into an input field in a form or in an HTML
element respectively. Example: $("#username").val("Mohammed"); //allows to write the string
Mohammed in the text input field of a given form.
You can then call your function like this: var result = addTwoIntegers(5,6); // which returns 11
When you call myFunction you should give the right name of the function that will be the callback
function. Example:
…..
myFunction(15, testIfGreaterThanTen); // which will display “15 is greater than 10”
…..
var testIfGreaterThanTen = function(param){
return(param>10);
}
To write a text after an HTML element (on the same line) you have to use the jQuery after()
function. Example:
Please use HTML and jQuery only to add interactivity to webpage given in this homework
named “hw2.html”. Form validation is an important process to ensure that all data entered by
the user provided for each required field. Using jQuery, modify the given website to allow it to
notify the user when the input is incorrect.
Inside the file “validate.js”, you will find two functions: validateField which should take in three
parameters: fieldElem, infoMessage, and validateFn. fieldElem can be a CSS selector, jQuery
element, or DOM element that represents a single form text field. infoMessage should be a
string that gives a human-readable description of the field’s requirements. validateFn should be
a function that validates the field’s value.
The function validateField should be called when the user focuses on one of the three form’s
input fields. The infoMessage should be displayed after (on the same line) the input field the
user is focusing on. The infoMessage should inform the user about the rules to be respected for
the current input field. When he focuses on another input field the infoMessage of the previous
input field disappears and the infoMessage of the current input field appears.
The callback valideFn should be replaced by one of the three functions validateUsername,
validatePassword and validateEmail when the validateField is called.
If the three input fields are well filled in by the user, the submit button becomes enabled.