Answered step by step
Verified Expert Solution
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)
- 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
- Must be of the form a@b.c with exactly one @ symbol and exactly one dot (period) appearing somewhere after the @.
- 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.
- Before validating the email address, remove any spaces the user might have added before or after it. Use s = s.trim() to do this.
- 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?
- 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.
- You don't have to check if the other characters are letters.
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started