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.
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;
}
}
ASKER
I'm lost, please source code for my case. )please note that control_ID is string and not number.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
Hmm...
http://snook.ca/archives/javascript/testing_for_a_v
http://stackoverflow.com/questions/1181575/javascript-determine-whether-an-array-contains-a-value
try
(array.toString.indexOf(va