Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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 answers
Answer question

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 Report

0

You can validate it like this:

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

0

You could use the following:

if(str.length && str.trim() === ""){
    return false
  }else{
    return true
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!