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

264
Visualizações
How can I difference id (number) and email (string) in Javascript?

I have API and some of them return to only email and some return only id. For example:

user1 value: example@example.com

user2 value: 1212391361783212

I need that if I have input and wanna give value if the value is email. If the value is id, the input value must be null

<input value={???}/>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can check if a string is a valid number using isNaN. So

if(isNaN(value)) {
    // value is email
 } else {
    // value is id
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

Your code sample is incomplete but what you're trying to do should look something like this. Rather than assuming any non-numerical response is a valid email, which can break in the future as the API changes, be explicit about what you're looking for as below.

// Assume you're comfortable using a relatively loose pattern to match an email i.e. no overly strict formatting rules
const mailPattern = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
// Assume response is in scope and contains the API response with the email or id deserialized from the body
const value = mailPattern.test(response) ? value : null
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Regular expression /^\d+$/ combined with RegExp.prototype.test():

if (/^\d+$/.test(value)) {
  // It's number
}
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