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

178
Visualizações
How can we check seperately true/false if empty string [eg: ' '] or string contain only white spaces [eg: ' ']?

Hi team Iam new to react and javascript. Iam working on some validation parts. I need to take input only if it is either an empty string or a non empty string but it shouldn't be only white spaces..? how can I check that condition?

var element='' // this should take 
var element='       ' // this shouldnot (bcz contain only empty space) 
var element='Hai how are you'  // this should take
var element='  jji       hooy      ' // this should also take

Can someone help i used trim() but if so it cant identiy '' and ' '

any other solution

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

0

You can use a regular expression to match strings that contain only whitespace.

!/^\s+$/.test('');    // true
!/^\s+$/.test('   '); // false
!/^\s+$/.test('Hai how are you');        // true
!/^\s+$/.test('  jji       hooy      '); // true
  • ! means ‘not’
  • ^ means ‘start of string’
  • $ means ‘end of string’
  • \s+ means ‘whitespace, one or more times’
about 4 years ago · Juan Pablo Isaza Relatório

0

You can validate it like this:

validate(element) {
    return element.length && element.trim().length;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could use the following:

if(str.length && str.trim() === ""){
    return false
  }else{
    return true
  }
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