Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Validate Email Address (filename: validateemailaddr.html ) Write a function named isValidEmailAddr(emailAddr) that takes an email address as a parameter, checks if the email address is

Validate Email Address (filename: validateemailaddr.html)

  1. Write a function named isValidEmailAddr(emailAddr) that takes an email address as a parameter, checks if the email address is valid, and returns the result as a Boolean value. To be valid the email address must meet the following criteria
    1. Must be of the form a@b.c with exactly one @ symbol and exactly one dot (period) appearing somewhere after the @.
    2. There must be at least one character before the @, at least one character between the @ and the dot, and at least one character after the dot.

  1. Before validating the email address, remove any spaces the user might have added before or after it. Use s = s.trim() to do this.
  2. You will need to use the indexOf() and lastIndexOf() functions to find the locations of the @ symbol and the dot symbol.

Given a string, s, s.indexOf('@') will give you the leftmost index of the @ symbol. s.lastIndexOf('@') will give you the rightmost index. If the @ symbol is not present they will return -1. The functions will search for any parameter string you specify.

So, if you know the first and last index of '@', how can you tell if it only occurs once?

  1. Once you have confirmed that there is exactly one @ and exactly one dot, use their indices and the length of the string to confirm that the address is valid

For example, if the @ is the first character, the address is invalid and return false.

  1. You don't have to check if the other characters are letters.

  1. Write JavaScript code that asks the user to enter an email address from a dialogue box, calls the function isValidEmailAddr and displays the result in an alert message box.

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

More Books

Students also viewed these Databases questions