0

Hi i'm new to javascript and I'm trying to check if a new registered user on my site is not using a gmail / hotmail / or a yahoo

Here is what i have tried to check if the mail is not a gmail / hotmail / yahoo. But if the user is using one of them i still get the error and the console log:

example not using a gmail that still gets the error and console log

var testMail = 'testmail@fakemail.com';
var check_email = '[a-zA-Z0-9]{0,}([.]?[a-zA-Z0-9]{1,})[@](gmail.com|hotmail.com|yahoo.com)';

if (!testMail.check_email) {
  //errors.push({msg: "You can't use that email to register"});
  console.log("You can't use that email to register") // using console.log for testing
}

example using a gmail / hotmail / yahoo:

var testMail = 'testmail@gmail.com';
var check_email = '[a-zA-Z0-9]{0,}([.]?[a-zA-Z0-9]{1,})[@](gmail.com|hotmail.com|yahoo.com)';

if (!testMail.check_email) {
  //errors.push({msg: "You can't use that email to register"});
  console.log("You can't use that email to register") // using console.log for testing
}
  • You have to apply your regex somewhere. Yet should only check for `gmail.com|hotmail.com|yahoo.com`. [A correct email validation is just not possible](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript). – JavaScript Oct 27 '20 at 07:30

2 Answers2

0

You have a Regex pattern in check_email, but you are not making use of it anywhere. Try something like this:

var testMail = 'testmail@fakemail.com';
var check_email = '[a-zA-Z0-9]{0,}([.]?[a-zA-Z0-9]{1,})[@](gmail.com|hotmail.com|yahoo.com)';
var patt = new RegExp(check_email);
var result = patt.test(testMail);
if (!result) {
  //errors.push({msg: "You can't use that email to register"});
  console.log("You can't use that email to register") // using console.log for testing
}
gkulshrestha
  • 855
  • 1
  • 6
  • 11
-1

You can use search() method and set check_email as parameter.

 var testMail = 'testmail@fakemail.com';
    var check_email = '[a-zA-Z0-9]{0,}([.]?[a-zA-Z0-9]{1,})[@](gmail.com|hotmail.com|yahoo.com)';
    
    if (!testMail.search(check_email)) {
      //errors.push({msg: "You can't use that email to register"});
      console.log("You can't use that email to register") // using console.log for testing
    }
cabrillosa
  • 155
  • 8