0

This is the email validation coding.

I only know to do example 5, the email length must over 5 strings, otherwise, it will become error and remind people to re-write the email.

I don’t know how to write the example 2-4 based on the email requirements. Kindly please give me some advices. Thank you so much!

function validateEmail(email) {
    console.log(email)
  // email requirements as below:
  // email length >= 5 character
  // @ cannot be the first character
  // @ must before .
  // . cannot be the last character
  // example 
  // 1. a@a.com < return true
  // 2. @a.com < return false, throw TypeError: invalid email address
  // 3. a.b@com < return false, throw TypeError: invalid email address
  // 4. a@a.com. < return false, throw TypeError: invalid email address
  // 5. a@a.c < return false, throw TypeError: e-mail address too short
  // 6. a@.com < return ture
  // example 5, email length >= 5 character
  if(email.length<5) {
    throw new TypeError(`e-mail address too short`)
  }
  // example 4,  . cannot be the last character
  
  }
// example 1,6
    return true

    function newFunction() {
        return "'"
    }
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Validating email addresses is a notorious and surprisingly difficult problem. It's best to use a fully-debugged package if you are building a production system. If it's a student exercise: a regex or a parser are in your near future. – O. Jones Oct 25 '21 at 13:04
  • You might what to have a look at [`String.prototype.indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) for retrieving the position of a single character within a string – secan Oct 25 '21 at 13:07

1 Answers1

0

You can follow this REGAX for validate email. Please check

const emailToValidate = 'a@a.com';
const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
console.log(emailRegexp.test(emailToValidate));

Thanks

Manish Kumar
  • 111
  • 6