Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.In the JavaScript file, note that three functions are supplied. The $ function. The validatePhone function that contains the validation code. And an onload event

1.In the JavaScript file, note that three functions are supplied. The $ function. The validatePhone function that contains the validation code. And an onload event handler that attaches the validatePhone function to the click event of the Validate button.

2. Change the regular expression pattern in the pattern variable so the phone number can contain an optional 1- prefix. The best way to do this is to copy the pattern variable to a new line and then comment out the original. This way, you can refer to the original pattern as you adjust it.

3. When the validation in step 4 is working correctly, change the new pattern so that the phone number can also contain either dashes or periods. Again, its best to make a copy so you can refer to what came before.

4. When the validation is step 5 is working correctly, change the new pattern so the phone number can have optional parentheses around the area code. To accommodate this change, youll want to allow blank spaces instead of dashes or periods after the optional 1 and after the area code.

html part:

Phone Number Validator

Validate phone number

This is the JS code to work on:

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

var validatePhone = function() { var phone = $("phone").value; var pattern = /^\d{3}-\d{3}-\d{4}$/; // 999-999-9999 var isValid = pattern.test(phone); $("message").firstChild.nodeValue = (isValid) ? "Valid phone number" : "Invalid phone number"; };

window.onload = function() { $("validate").onclick = validatePhone; $("phone").value = "123-456-7890"; // set default phone number };

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

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions