1

I have array like the below

data = [
  [product, billdetail],
  [avn, 200],
  [plioc,3000],
  [myjio,4000]
]


for(var i = 1; i < data.length; i++) {
            var cube = data[i];
            for(var j = 0; j < cube.length; j++) {

                console.log("cube[" + i + "][" + j + "] = " + cube[j]);
                 if(cube[j] === "")
                 {
                     alert('empty value');

                 }
            }
        }

I am doing empty validation here, i also want validation like product should have only alphabets and billdetail should have number only.so how can i achieve that here.please help me for the same. (consider first row is table header and other rows are values.)

4
  • So you want to check each value against a set of rules? Can you clearly state what those rules are, and what you have tried so far Commented Feb 28, 2020 at 6:51
  • I am checking if any of the field is empty . i also required to check like product has only alphabet and billdetail has only number. Commented Feb 28, 2020 at 6:53
  • 1
    And what have you tried so far? What approach do you think might be needed? Commented Feb 28, 2020 at 7:04
  • I have to iterate through each value using loop to check each value in the array corresponding to first row and validate Commented Feb 28, 2020 at 7:06

1 Answer 1

2

There are many ways to do this, one of is below.

data = [
  ['product', 'billdetail'],
  ['avn', 213],
  ['plioc',3000],
  ['myjio',4000],
  ['inval1d produc1', 'invalidbill']
]

for (let i = 1; i < data.length; i++) {
  let product = data[i][0];
  let bill = data[i][1];
  
  if (!product || !bill) {
    console.log('Product or Bill is null', product, bill);
  }
  
  if (!product.match(/^[A-Za-z]+$/)){
    console.log('Invalid Product:', product);
  }
  
  if (typeof(bill) !== 'number') {
    console.log('Invalid Bill:', bill);
  }    
}

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

3 Comments

create a zero based for loop, right now its throwing an error
I think the post mentioned that the first array in the data are headers right ? But anyways, what is the error ?
sorry, i dint read the header part , m good, also its not throwing any error, what i wanted to say was that it is skipping first record.

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.