kensington
kensington

Reputation: 27

how to check multiple conditions at the same time

I have a function where I check the length and wether the input isnt empty, I would like to know if its possible to check at the same time wether the input is a number or not. Im looking for something like this: if (field.value.length > 0 && isNaN)

function checkNotEmpty(field,span){

                    if (field.value.length > 0){
                    document.getElementById(span).className='ok'
                    document.getElementById(span).innerHTML='its ok';

                    }
                    else {
                    document.getElementById(span).className='notok'
                    document.getElementById(span).innerHTML='its not ok';

                    }
                }

Upvotes: 1

Views: 1016

Answers (2)

Ju66ernaut
Ju66ernaut

Reputation: 2691

Unless I am missing something, it appears you already have your answer.

if (field.value.length > 0 && isNaN(field.value)) {
    //...value is at least 1 character long and field IS not a number
}

Upvotes: 1

Yoric
Yoric

Reputation: 4083

Are you looking for this?

function checkNotEmpty(field, span) {
  let value = parseInt(field.value);
  if (isNaN(value) || value < 0) {
    // Either it's not a number or it's negative.
    // React appropriately.
  } else {
    // We're good.
  }
}

Upvotes: 0

Related Questions