Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
check if string does not contain values

I am using indexOf to see if an email contains anything other than a particular text.

For example, I want to check if an email DOES NOT include "usa" after the @ symbol, and display an error message.

I was first splitting the text and removing everything before the @ symbol:

var validateemailaddress = regcriteria.email.split('@').pop();

Then, I check if the text doesn't include "usa":

if(validateemailaddress.indexOf('usa')){
    $('#emailError').show();
} 

Something with the above check doesn't seem right. It works - I can enter an email, and if it does not include 'usa', then the error message will show.

Regardless, when I add an additional check, like if the email does not include "can", then the error message shows no matter what.

As follows:

if(validateemailaddress.indexOf('usa') || validateemailaddress.indexOf('can')){
    $('#emailError').show();
} 

As stated, using the above, the error message will show regardless if the email includes the text or not.

All I want to do is check if the email includes 'usa' or 'can', and if it doesn't, then show the error message.

How can I make this work?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Here is a simple JavaScript function to check if an email address contains 'usa' or 'can'.

function emailValid(email, words) {
    // Get the position of @ [indexOfAt = 3]
    let indexOfAt = email.indexOf('@');

    // Get the string after @ [strAfterAt = domain.usa]
    let strAfterAt = email.substring(indexOfAt + 1);

    for (let index in words) {
        // Check if the string contains one of the words from words array
        if (strAfterAt.includes(words[index])) {
            return true;
        }
    }

    // If the email does not contain any word of the words array
    // it is an invalid email
    return false;
}

let words = ['usa', 'can'];

if (!emailValid('abc@domain.usa', words)) {
    console.log("Invalid Email!");
    // Here you can show the error message
} else {
    console.log("Valid Email!");
}

about 4 years ago · Juan Pablo Isaza Relatório

0

There are several ways to check, if a string contains/does not contain a substring.

String.prototype.includes

'String'.includes(searchString); // returns true/false

String.prototype.indexOf

// returns values from -1 to last postion of string.
'String'.indexOf(searchString); 

// In combination with ~ this can work similar to includes()
// for strings up to 2^31-1 byte length

// returns 0 if string is not found and -pos if found.

~'String'.indexOf(searchString); 

With the help of Regular Expressions:

// substring must be escaped to return valid results
new RegExp(escapedSearchString).test('String'); // returns true/false if the search string is found

'String'.match(escapedSearchString); // returns null or an array if found

So overall you can use allmost all methods like:

if ('String'.function(searchString)) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}

Or in case of indexOf:

if ('String'.indexOf(searchString) > -1) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}
// OR
if (~'String'.indexOf(searchString)) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like that, using includes:

const validateEmailAdress = (email) => {
  const splittedEmail = email.split('@').pop();
  
  return (splittedEmail.includes('usa') || splittedEmail.includes('can'))
}

console.log("Includes usa: ", validateEmailAdress("something@gmail.usa"))
console.log("Includes can: ", validateEmailAdress("something@gmail.can"))
console.log("Does not includes: ", validateEmailAdress("something@gmail.com"))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda