Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script doesn't work

Status
Not open for further replies.

cb49747

MIS
Apr 23, 2002
181
US
Here is a script I wrote.

Code:
<SCRIPT LANGUAGE="JavaScript">
 function CheckAccess(f) { 
  var allowableInvestors = new Array();
  allowableInvestors["001"]="001";
  allowableInvestors["004"]="004";
  if (f != allowableInvestors[f]) { 
   alert ("I'm sorry, you do not have proper rights to give access to investor " + f + " thank you."); 
   addaccess.iaccinv.value = " "; 
  }
 }
</SCRIPT>

the form name is addaccess and the field name is iaccinv. however I do have 5 fields all with the same name. as shown below. I need the fields to have the same name.

Code:
   <form name="addaccess">
    <tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>
<tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>
<tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>
<tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>
<tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>
<tr><td class="dcolor"><b>Investor: </b><input type="text" size="20" name="iaccinv" onChange="CheckAccess(this.value)"></td><td width="5" class="dcolor"></td><td class="dcolor"><b> Pool: </b><input type="text" size="20" name="iaccpool"></td></tr>

if I enter a number not in the array it should bring up a alert window and reset the field to blank. When I run the above with a bad number I get the alert but the field does not get reset.

Any suggestions?
 
Why do you need the fields to all have the same name?
This causes a lot of problems later on whose solutions are about the same as not using identical names to begin with.

In any event, if you name multiple fields with the same name then the data for those fields becomes an array.
So you end up with an array for field iaccinv and one for iaccpool.
You would have to get the array object for that field name and loop through it testing the values. For the test to be meaningful you would need to know how to relate the array position to a specific field on the form anyway and if you need to do that you could just as easily have different names for the fields and not have to loop through the array.

Explain why you need it that way and we can probably tell you a good approach for your needs.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Well it starts with 5 fields, however if you have allready added some numbers such as 3 of them next time there will be 8 fields. the three existing plus the 5 empty ones. Since I never know how many fields there will be I name them all the same and then just delete all existing entries and then add all entries from the array. Has been working real well. I also do a check on the next page before adding the numbers that a person has the rights to that number to add it. However if they don't they have to go thru and do it again. Anyway I wanted to add some java script to let people know prior to submiting the form that a number they have entered is invalid. This works with the exception of not clearing out the existing field on alert of a bad number.

Hopes this make since.

Thanks for listining
 
Change all occurrences of this:

Code:
CheckAccess(this.value)

to this:

Code:
CheckAccess(this)

then change your function to this:

Code:
function CheckAccess(f) {
	var allowableInvestors = new Array();
	allowableInvestors['001'] = '001';
	allowableInvestors['004'] = '004';
	if (f.value != allowableInvestors[f.value]) {
		alert('I'm sorry, you do not have proper rights to give access to investor ' + f.value + ' thank you.');
		f.value = ' ';
	}
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
BillyRayPreachersSon,

Thank you a million. That worked perfectly, even better than I had hoped as I figured when ever a wrong number was entered all fields would be blanked out, however doing it the way you suggested only resets the wrong one. Perfect.

Thanks Again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top