Link to home
Start Free TrialLog in
Avatar of Abdu_Allah
Abdu_Allah

asked on

How to check if the following array if an item exist or not

Hi, please consider the following code, how can I check if Control_ID is already exist in objValidationOutput array if so I should not add it to the array again.

Help please.
function Validate(obj) {
for (var i = 0; i < obj.length; i++) {
        if (obj[i].Validation_Type == 'Required') {
              if (document.getElementById(obj[i].Control_ID).value == '') {
                                                   objValidationOutput[objValidationOutput.length] = new ValidationArray();
                                                   objValidationOutput[objValidationOutput.length-1].Control_ID = obj[i].Control_ID;
                                                   objValidationOutput[objValidationOutput.length-1].Error_Message = obj[i].Error_Message;
                                                   objValidationOutput[objValidationOutput.length-1].Validation_Type = obj[i].Validation_Type;
                                                }
                                            }

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Avatar of Abdu_Allah
Abdu_Allah

ASKER

I'm lost, please source code for my case. )please note that control_ID is string and not number.)
ASKER CERTIFIED SOLUTION
Avatar of magedroshdy
magedroshdy
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about

function Validate(obj) {
  for (var i = 0; i < obj.length; i++) {
    if (obj[i].Validation_Type == 'Required') {
      var id = obj[i].Control_ID;
      if (objValidationOutput[id] || document.getElementById(id).value != '') continue;
      objValidationOutput[id] = new ValidationArray();
      objValidationOutput[id].Error_Message = obj[i].Error_Message;
      objValidationOutput[id].Validation_Type = obj[i].Validation_Type; // this will ALWAYS be == "Required" so perhaps ignore?
    }
  }
}


now you can do

for (var o in objValidationOutput) { // o is what used to be objValidationOutput[i].Control_ID
  alert(o+':'+objValidationOutput[o].Error_Message;
}

Open in new window