Programming in HTML5 with JavaScript and CSS3: Access and Secure Data
- 8/25/2014
Answers
This section contains the solutions to the thought experiments and answers to the objective review questions in this chapter.
Objective 3.1: Thought experiment
You’ve already seen how to get access to the DOM and modify elements through JavaScript. By using these techniques, you can get a reference to the input controls on the form and, based on user input in certain elements, this can trigger modification to the validation rules (for example, change the regular expression validation dynamically). You can get regional context about a user from the Geolocation API. From this you can derive what part of the world the user is in and apply the exact validation on the input controls.
Objective 3.1: Review
Correct answer: C
- Incorrect: A radio button is suited for allowing a single selection.
- Incorrect: A text area is suited for a multi-line text box.
- Correct: Check boxes allow multiple selections.
- Incorrect: A radio button doesn’t allow more than one selection.
Correct answer: D
- Incorrect: A text box allows data entry but is plainly visible.
- Incorrect: A text area allows data entry but is plainly visible.
- Incorrect: url is a type of text box with special validation rules.
- Correct: A password input type hides the characters being entered.
Correct answer: B
- Incorrect: A button is generic and must have an event handler to perform custom logic.
- Correct: The submit button invokes the forms submit action.
- Incorrect: The reset button clears all input fields on the form.
- Incorrect: A radio button is used for a selection list.
Correct answer: C
- Incorrect: You can do this with a custom event, but that’s more work than necessary.
- Incorrect: The goal is to validate the data before submitting the form.
- Correct: The required attribute ensures that a field contains a value before being submitted.
- Incorrect: A label would be informative but doesn’t guarantee that all the required fields are populated before submitting.
Objective 3.2: Thought experiment
The safest approach to restricting input data is to restrict the characters that a user can enter into a specific field. If a field is designed to accept only numeric data, ensure that the validation on that input control will allow only numeric data. The same is true for dates, text, and any other input that a user can freely type into. Regular expressions simplify this type of validation by verifying that only the expected characters are entered. If a text box is expecting a person’s name, don’t allow HTML characters such as the < or > symbols to be input into the field. Also, restrict the field length so that it matches the type of the data expected. A field expecting the age of a person doesn’t need to be 500 characters; you can probably get away with allowing only 3 characters.
Objective 3.2: Review
Correct answer: A
- Correct: The $ sign denotes the end of the string.
- Incorrect: The % sign doesn’t denote the end of the string.
- Incorrect: The ^ character denotes the start of the string.
- Incorrect: The & character doesn’t denote the end of the string.
Correct answer: D
- Incorrect: Allows scripts to run
- Incorrect: Only allows content from the same origin
- Incorrect: Allows forms
- Correct: Allows content from the containing HTML document
- Incorrect: Not a valid option
Correct answer: D
- Incorrect: Credentials aren’t passed with the open method.
- Incorrect: Credentials aren’t passed with the request method.
- Incorrect: Ready state is a property that indicates the current state of the connection.
- Correct: Credentials are passed only if the server requests them with a return code 401.
Objective 3.3: Thought experiment
The task assigned here to build a scroll across the top of the page is seen in many websites today. A stock price ticker is a typical application of this. This solution would potentially incorporate different technologies. At its core, you can implement the XMLHttpRequest object to make a call to an API that provides stock data. When the data is retrieved, you can display in the browser. Because the solution calls for not posting the entire page, you would need to use dynamic DOM manipulation to display the results and have them scroll across the top of the page. Because the quotes must be updated regularly, you would likely include the use of a timer to poll for the results at a regular interval.
Objective 3.3: Review
Correct answer: D
- Incorrect: A JSON string isn’t just a comma-separated list.
- Incorrect: A JSON string isn’t a list delimited by semi-colons.
- Incorrect: A JSON string isn’t a list delimited by semi-colons.
- Correct: A JSON string is a series of name/value pairs where the name of the property is followed by a colon and a quoted string. Multiple name value pairs are comma separated.
Correct answer: C
- Incorrect: Response doesn’t provide any direct information.
- Incorrect: responseBody provides the result in binary format.
- Correct: responseText provides the result as text that’s human readable.
- Incorrect: responseXML isn’t a valid property.
Correct answer: D
- Incorrect: Credentials aren’t passed with the open method.
- Incorrect: Credentials aren’t passed with the request method.
- Incorrect: Ready state is a property that indicates the current state of the connection.
- Correct: Credentials are passed only if the server requests them with a return code 401.
Objective 3.4: Thought experiment
In this application, you now need to know when users finish entering information into a field. You can use the onblur event for this. By hooking up onblur to the email field, you can use the XMLHttpRequest object to send a request to the server to validate that the address is unique and hasn’t been used before. The results of the data evaluation on the server are passed back in the response and can be used to highlight to users that the data isn’t unique. This provides a much better user experience in that users don’t need to wait until they fill out the entire form to have all the fields validated. Did you remember to encode the data before submitting it to the server to prevent an injection attack?
Objective 3.4: Review
Correct answer: C
- Incorrect: This isn’t a valid method on the JSON object.
- Incorrect: This method is used to serialize an object into a JSON string.
- Correct: This method is used to deserialize a JSON string into an object.
- Incorrect: This isn’t a valid method on the JSON object.
Correct answer: D
- Incorrect: ‘binary’ isn’t a valid option for the responseType.
- Incorrect: ‘image/jpg’ isn’t a valid option for the responseType.
- Incorrect: type isn’t a valid property name on the response object.
- Correct: The response object’s responseType property must be set to ‘blob’.
Correct answer: B
- Incorrect: A submit button submits the entire form to the server by default.
- Correct: Handling the submit event on the form allows you to intercept the form before submitting and perform custom actions with it.
- Incorrect: The action attribute indicates what server-side page the form should submit.
- Incorrect: All elements on the form should have a name to use jQuery to serialize them. However, this has no effect on form submission.