Purchase Solution

Algorithm to validate a US phone number (xxx) xxx-xxxx

Not what you're looking for?

Ask Custom Question

Design an algorithm (no code) just using if-then statement or while statement generically, that validates a US phone number in the form of (xxx) xxx-xxxx.

In many situations, information is supplied as a string of text characters. The characters of that information may need to be in a particular format to be valid. Common examples include credit card numbers, e-mail addresses, dates, and telephone numbers.

Develop an algorithm to verify if a string of characters could represent a valid U.S. phone number. Since determining whether a string of characters is actually a valid phone number is a complex process. Therefore, you will determine only if the characters in the string appear in a format appropriate for such a phone number.

The algorithm should accept any string in the form (xxx) xxx-xxxx, where x represents a digit and the other characters represent themselves, and reject all others. For this algorithm, you can determine the total number of characters in the string and inspect the value of one character at a time.

Provide an example of an input string that is in the proper format and an example that is not in the proper format. Describe how your algorithm determines that the first string is in the proper format and that the second string is not in proper format.

Purchase this Solution

Solution Summary

Algorithm is composed of three steps. Solution considers 3 cases of improper format US phone numbers to illustrate failure at each of these steps, and a case of proper format US phone number.

Solution Preview

Please consider this response as a mere guidance for you and use it for understanding the logic. Express it in your own words/style/format in case you want to use it for submission.

procedure checkPhoneNumber
Input: String phoneNumber
Output: Boolean value OK or NotOK

// Assumption: A valid phone number is exactly 14 characters long as per the format "(xxx) xxx-xxxx"
// where x indicates a digit.

Step 1: Check the length of input (argument) phone number.

if (stringLength(phoneNumber) != 14)
return NotOK

// Reaching here means input phone number is exactly 14 characters ...

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Javscript Basics

Quiz on basics of javascript programming language.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.