When registering an email, I need to know if it is not in an array of spam data which cannot be used to register an email. My question is how exactly the function works indexOf
: I send as [email protected] email and if it matches, how come it compares only the left part of the @, still not having the domain added in the array?
this.checkEmail("[email protected]");
checkEmail(email: string) {
let emails = [
"@outlook.co",
"@otlook.es",
"@hahoo.com",
"@YAOO.COM.MX",
"@yahoo.com.mex",
"NODIOCORREO",
"NOMBRE",
"NOTENGO",
"NOVALIDO",
"PACHO"
];
for (var i = 0; i < emails.length; i++) {
if (email.indexOf(emails[i]) !== -1) console.log("encontrado");
}
The example above returns found even though it only matches the word NOTENGO and not the full email [email protected] .
The indexOf function searches for a substring within a string and returns the beginning of the found substring, if it doesn't find the substring it will return -1. Which means that when you send "name" to indexOf as a substring it will look for "name" in your string and return the index of where it found the "n" in the string.
My advice is to fill the arrays with the usernames and their email type and also create another array to check if the domain name is correct:
And when doing the verification you just have to do as they told you in another answer, split the @ sign and verify that the second part matches the valid emails.
You have it backwards. The method
indexOf
must traverse the array, not the string to search for. In addition, it is not necessary to do a for to go through the array, that is already done by the indexOfChange it to this:
.indexOf
returns the index where it isemail
in the arrayemails
. And if it doesn't find it, it returns -1What you are trying to do is fine, but before validating the indexof you have to split the email if you only want to evaluate the part of the name of the email and not the domain of the email, being as follows:
the problem as indicated is that this does not evaluate the domain part and from what I see in your example you have domains; if you want to evaluate also the domains you would have to use two indices:
split documentation
If you want to test not only that it is equal but you want to test if the mail contains a sub-string then you can turn to includes
New Question from the OP in the comments: Why is the domain returned not found, my question is why? *
Answer: in the examples I'm giving I'm using the array you shared; which contains domains or email names, and not an exact email. when an email is passed, 1 or both parts of the email are simply validated individually and not an exact email; to evaluate a string is a valid email and is not on the list; your list must also contain the exact email you want to block and the string must contain the parttern regex as demonstrated in this other question/answer from the site: https://stackoverflow.com/a/46181/17161735 but in your post You have never asked how to validate that a string is an email or has the format of an email.