[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-04
6
Medium Priority
?
251 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:Abdu_Allah
6 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26178134
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 26178187
I'm lost, please source code for my case. )please note that control_ID is string and not number.)
0
 
LVL 6

Accepted Solution

by:
magedroshdy earned 1600 total points
ID: 26178201
try the following
function Validate(obj) {
for (var i = 0; i < obj.length; i++) {
        if (obj[i].Validation_Type == 'Required' && objExists(document.getElementById(obj[i].Control_ID)== false) {
              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;
                                                }
                                            }
 
function objExists(Control_ID) {
var exists = false;
   for(var i=0 ; i< objValidationOutput.length; i++){
     if(objValidationOutput[i].Control_ID == Control_ID){
exists = true;
}

   }
return exists;
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 400 total points
ID: 26178208
try this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
	<script>
		function check(str)
		{
			var arr = new Array();
			arr = ["Sam","Great", "Sample", "High"]
			if ( arr.toString().indexOf('Sam') != -1 )
			{
				alert("value is available");
			}
			//alert(arr.toString().indexOf('Sam'))
		}
	</script>

</HEAD>

<BODY BGCOLOR="#FFFFFF" onload='check("asd")'>
	
</BODY>
</HTML>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26178413
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

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26179040
Hmm...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question