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
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’You can validate it like this:
validate(element) {
return element.length && element.trim().length;
}
You could use the following:
if(str.length && str.trim() === ""){
return false
}else{
return true
}