Jenny Mok Jenny Mok - 20 days ago 5
Javascript Question

Operator for validation for 2 options

I have a situation where I'm confused how to use if else statement. It's related to validation.

My below code worked for the first 2 field.

if(!name || name=='' &&
!age || age==''
){
console.log('failed to validate');
return false;
}


my 3rd field is the tricky one. They are
skipped
or
passed
. The field will appear either
skipped
or
passed
. How do I ensure they got validated? I can't do
!skipped || skipped == '' && !passed || passed == ''
this surely will failed.

Answer

You could try something like this:

if(!name || name=='' 
  && !age || age=='' 
  && ((!skipped || skipped == '') 
      || (!passed || passed == ''))
  ){
  console.log('failed to validate');
  return false;
}