Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use a Switch statement to enhance code that validates a date In this exercise youll improve the code that validates a date in the Account

Use a Switch statement to enhance code that validates a date

In this exercise youll improve the code that validates a date in the Account profile application. When youre done, the application will correctly validate dates like 2/30/2017 or 11/31/2017. The interface look this:

1. Open the HTML AND JavaScript files, Then run the application to see the user interface shown above.

2. Enter values the for all the values on the form. For the date of birth field, enter the invalid date 11/31/1980, and click save. Note that the application accepts this date as valid.

3. Change the Date of Birth to 13/31/1980, and click save. Note that this time the application doesnt accept the date.

4. In the JavaScript file, note that the ready event handler contains an isDate() function and a handler for the click event of the save button that contains the validation code.

6. Code a case label for the value 2 that returns false if the value of the day variable is greater than 28. Note that this doesnt handle leap years, but thats OK for this exercise.

7. Code case labels for the values 4, 6, and 9 that fall through to the case label for 11. The code for case label 11 should return false if the value of day is greater than 30.

8. Finally, Code a default case that returns false if the value of day is greater than 31.

9. Run the application and test it with the valid dates from steps 2 and 3. The application should now correctly identify both of these dates as invalid.

Please check the files bellow:

HTML File:

My Account Profile

My Account Profile

CSS File:

My Account Profile

My Account Profile

.js File:

"use strict";

$(document).ready(function(){

var isDate = function(text) {

if( ! /^[01]?\d\/[0-3]\d\/\d{4}$/.test(text) ) return false;

var index1 = text.indexOf( "/" );

var index2 = text.indexOf( "/", index1 + 1 );

var month = parseInt( text.substring( 0, index1 ) );

var day = parseInt( text.substring( index1 + 1, index2 ) );

if( month < 1 || month > 12 ) { return false; }

if( day > 31 ) { return false; }

return true;

};

$( "#save" ).click(function() {

$("span").text(""); // clear any previous error messages

var isValid = true; // initialize isValid flag

var email = $("#email").val();

var phone = $("#phone").val();

var zip = $("#zip").val();

var dob = $("#dob").val();

if ( email === "" ||

!email.match(/^[\w\.\-]+@[\w\.\-]+\.[a-zA-Z]+$/) )

{

isValid = false;

$( "#email" ).next().text("Please enter a valid email.");

}

if ( phone === "" || !phone.match(/^\d{3}-\d{3}-\d{4}$/) ) {

isValid = false;

$( "#phone" ).next().text(

"Please enter a phone number in NNN-NNN-NNNN format.");

}

if ( zip === "" || !zip.match(/^\d{5}(-\d{4})?$/) ) {

isValid = false;

$( "#zip" ).next().text("Please enter a valid zip code.");

}

if ( dob === "" || !isDate(dob) ) {

isValid = false;

$( "#dob" ).next().text(

"Please enter a valid date in MM/DD/YYYY format.");

}

if ( isValid ) {

// code that saves profile info goes here

}

$("#email").focus();

});

// set focus on initial load

$("#email").focus();

});

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions