1

I want to check if there is any empty field or not before submitting form. But the problem is I have variable number of fields (value1,value2...) So i can not use simple if statement.

So is there any way i can archive this.

state1 = {
                vale1:'',
                vale2:'',
                vale3:'',
                vale4:''
            }

state2 = {
                vale1:'21',
                vale2:'a',
                vale3:'f',
                vale4:''
            }
state3 = {
                vale1:'fdf',
                vale2:'fgf',
                vale3:'gf',
                vale4:'hg'
            }

state4 = {
                vale1:'21',
                vale2:'a',
                vale3:'f'
            }
  functionIWant(state1) ----> false 
  functionIWant(state2) ----> false
  functionIWant(state3) ----> true
  functionIWant(state4) ----> true

Is there any functionIWant ?

10
  • To increase the chances of receiving any answers, please provide your attempt on the function. None of the built-in functions will do the trick. Commented Sep 12, 2018 at 15:10
  • You want a for loop, that will go through all values regardless of the number, i.e. you do not have to know how many values you have coming in for it to work. Commented Sep 12, 2018 at 15:11
  • do you have numbers as values? is zero a wanted value? Commented Sep 12, 2018 at 15:15
  • @Nina Scholz value can be any but not ''. Commented Sep 12, 2018 at 15:17
  • do you have only strings as values? your question asks about null, undefined and empty string ''. please add what type you have and what you want as valid value. Commented Sep 12, 2018 at 15:20

4 Answers 4

4

You could get the values and check every with boolean.

function check(object) {
    return Object.values(object).every(Boolean);
}

var state1 = { vale1: '', vale2: '', vale3: '', vale4: '' },
    state2 = { vale1: '21', vale2: 'a', vale3: 'f', vale4: '' },
    state3 = { vale1: 'fdf', vale2: 'fgf', vale3: 'gf', vale4: 'hg' },
    state4 = { vale1: '21', vale2: 'a', vale3: 'f' };

console.log(check(state1)); // false
console.log(check(state2)); // false
console.log(check(state3)); //  true
console.log(check(state4)); //  true

Sign up to request clarification or add additional context in comments.

Comments

2

You can use javascript's version of foreach and check if any of the key value pairs have empty or null values:

functionIWant(state){
    //boolean to return from function, initialize to true
    let notEmpty = true;
    //Iterate the state object passed in and use javascript's version of `foreach`
    //to check if any of the key value pairs have an empty or null value
    for (let key in state){ 
        if(state[key] === null || state[key] === ''){
             notEmpty = false;
             break;
        }
    }

    return notEmpty;
}

Comments

1

You can use Array#some() method over the object values:

function functionIWant(obj) {
  return !Object.values(obj).some(v => !v || v == '');
}

Demo:

function functionIWant(obj) {
  return !Object.values(obj).some(v => !v || v == '');
}

const state1 = {
  vale1: '',
  vale2: '',
  vale3: '',
  vale4: ''
}

state2 = {
  vale1: '21',
  vale2: 'a',
  vale3: 'f',
  vale4: ''
}
state3 = {
  vale1: 'fdf',
  vale2: 'fgf',
  vale3: 'gf',
  vale4: 'hg'
}

state4 = {
  vale1: '21',
  vale2: 'a',
  vale3: 'f'
}
console.log(functionIWant(state1)) 
console.log(functionIWant(state2)) 
console.log(functionIWant(state3)) 
console.log(functionIWant(state4)) 

Comments

1

You can create a function that walks in the object properties by doing:

state1 = {
  a: '',
  b: 'f',
  c: ''
}

state2 = {
  a: 'd',
  b: 'f',
  c: 'g'
}

function isValid(state) {
  return Object.values(state).every(val => val != '');
}

console.log(isValid(state1));
console.log(isValid(state2));

Object.values // returns all object values in an array.

every // you apply the predicator in all values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.