How to get vbscript and javascript variables to work together

I’m having a problem getting javascript and vbscript to place nicely together. Here is what I want to do:

I’m getting values from my database for different exam types. Here are some sample values:

Exam Type1
Exam Type2
Exam Type3

Here is how I get the values in vbscript:

<% 'Query to print out available exam types for the user to see

set my_rs = server.createObject("ADODB.recordset")
				
my_rs.open my_sql, my_conn				
			
dim existingExamCat
existingExamCat = ""

if not(my_rs.bof and my_rs.eof) then	
							
        do while not (my_rs.eof)					
  response.write my_rs.fields("examTypeCat") & "&lt;br&gt;"
  existingExamCat = existingExamCat & my_rs.fields("examTypeCat") & ","								
my_rs.movenext
loop													
my_rs.close
						
else
   response.write "There are no available exam types at this time."
end if				
					
response.write "&lt;br&gt;&lt;br&gt;" & existingExamCat

set my_conn = nothing
set my_rs = nothing

%>

The above vbscript works fine and gives me the correct results.

I set up another variable called existingExamCat to keep track of all the exam categories in a comma-delimited list. This is at the bottom of my page.

<script language=“javascript”>

var existingExamCat = "&lt;%= existingExamCat %&gt;";	

</script>

Then at the top as part of an onClick event, I have this:

function validateUniqueType ()
{

var newType = document.formAddExamType.exam_newType.value;
var compareString;

'alert (newType);

}

I want to see if the variable “newType”, which is entered by the user in a text input box exists in the list of already populated exam names.

So existingExamCat = “Exam Type1,Exam Type2,Exam Type3,”

and the variable “newType” is working. I want to compare newType & existingExamCat. I’ve looked all around online and am getting frustrated. Any help would be appreciated. Thanks!

You want to see if newType exists in existingExamCat :


function validateUniqueType ()
{
  bReturn = false;
  var newType = document.formAddExamType.exam_newType.value;
  var aExistingTypes = existingExamCat.split(",");

  for( var i=0; i < aExistingTypes.length; i++ ) {
    if( newType == aExistingTypes[i] )
      bReturn = true;
      break;
    }
  }
  return bReturn;
}

NOTE that if you have any “natural” commas in the database field examTypeCat, this won’t work and you’ll need to pick a different delimiter, like ~ or something.

My form now compares properly, thanks! When I click the submit button (input type = button with the onClick event), it doesn’t submit my form. Any ideas? Also, If I hit the enter key, it bypasses the onClick event. Is there a way to add that in as an additional check to the onClick event?

Remove the onclick event from your submit button, and call the same function in onsubmit of the form, ie
<form method=“post” onsubmit=“return ValidateStuff();”…

Make sure you return true from the ValidateStuff function if you want your form to submit, and false if there was some validation error and you don’t want it to submit.

(onsubmit covers clicking the submit button, and hitting enter by the way)

I have another question… it’s regarding the same stuff above. I need to do the exact same thing for another page and when I just change the variable names to reflect the correct fields, the array is only keeping the very last element. So if I add in a new exam code, it only checks against the very last one and not the whole list. What am I doing wrong?

Nevermind, I got it now. Here’s the final solution that I got to work in case anyone else is trying to do the same thing and having problems getting it to work:

<script language=“javascript”>
function checkSelection()
{

var newCode = document.formAddExam.exam_code.value;
/* this is the value of my input field for the new code */

var existingCodesArray = totalExamCodeList.split(“,”);

if (newCode == "")
{
  alert("You must enter a unique exam code before proceeding. Empty spaces are not valid.");
}
else
{
   for (var i=0; i &lt; existingCodesArray.length; i++)
   {	
   if (newCode == existingCodesArray[i])
   {

      alert("That exam code already exists. Please enter a unique exam code.");
      return;	
       }	
   }

   document.forms[0].submit();

} // else

}
</script>

body text goes here, including my input fields for the form

<%
my_rs.open “SELECT blah blah blah”
’ I’m getting all the examCodeID values from the db

   dim existingExamCode
   existingExamCode = ""

if not(my_rs.bof and my_rs.eof) then	
		
    do while not (my_rs.eof) 									
        existingExamCode = my_rs.fields("examCodeID") & ","
        totalExamCodeList = totalExamCodeList & existingExamCode											
        my_rs.movenext
    loop
end if

response.write "Total List: &lt;br&gt;"
response.write totalExamCodeList												
my_rs.close
							
set my_conn = nothing
set my_rs = nothing	

%>

Then at the bottom someplace I have:

<script language=“javascript”>
var totalExamCodeList = “<%= totalExamCodeList %>”;
/*This value is being used up top in the checkSelection() function */
</script>