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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing Variable 1

Status
Not open for further replies.

glenmac

Technical User
Jul 3, 2002
947
0
0
CA
Hi I'm not sure this is the correct forum for this question but here goes. I am passing a value from a form to an ASP page that has some javascript on it to validate a form on the same ASP page. The problem I'm having is using the value from the ASP in the Javascript.Is there a way to use an ASP variable in a javascript. Hmm sounds confusing doesn't it? well here's some of my code.
Code:
Dim MyArray(4,1),pass,nam,i,test


 pass = request.form("Password")
 nam = request.form("UserID")

 MyArray(0,1) = "john"		
 MyArray(1,1) = "mark"
 MyArray(2,1) = "jim"
 MyArray(3,1) = "doug"
 MyArray(4,1) = "tom"

 MyArray(0,0) = "john12"		
 MyArray(1,0) = "mark12"
 MyArray(2,0) = "jim123"
 MyArray(3,0) = "doug12"
 MyArray(4,0) = "thomas"


	for i = 0 to 4						
	 If MyArray(i,1) + MyArray(i,0) =  nam + pass Then	
  	  test=true
          Exit For
	 End If
	next
	
	If test = true then
	  response.write "You are successfull"

on the same page I need to use the variable nam in a javascript.

	ID1=BobForm.usrNam.value;
       if(ID1 != nam)
	{
	alert("Please input a valid username!");
	event.returnValue=false;
	BobForm.usrNam.focus()
	BobForm.usrNam.select()
	}
Hope I've explained things well enough and someone can help. It would be much appreciated.
 
I think what you need to do is put the variable into a hidden input field in your form. Then check that hidden form field:

instead of
ID1=BobForm.usrNam.value;
if(ID1 != nam)

use

ID1=BobForm.usrNam.value;
name=BobForm.HiddenField.value;
if (ID1 != name)


Later,
mwa


 
The only way i know this is to response.write it all to the screen.

response.write &quot;<script language=&quot;&quot;JavaScript&quot;&quot;>&quot;
response.write &quot;ID1=BobForm.usrNam.value;&quot;
response.write &quot;if(ID1 != &quot;&quot;&quot; & nam & &quot;&quot;&quot;)&quot;
response.write &quot;{&quot;
response.write &quot;alert(&quot;Please input a valid username!&quot;);&quot;
response.write &quot;event.returnValue=false;&quot;
response.write &quot;BobForm.usrNam.focus()&quot;
response.write &quot;BobForm.usrNam.select()&quot;
response.write &quot;}</script>&quot;

Not sure if thats done right for what you need but thats how i've got it to work for me in the past, but mainly used in functions or links such as;

response.write &quot;<script language=&quot;&quot;JavaScript&quot;&quot;>&quot;
response.write &quot;function &quot; & objRs(&quot;HEAD_FUNC&quot;) & &quot;() {&quot;
response.write &quot;document.getElementById('d2').innerHTML = &quot;&quot;&quot; & objRs(&quot;HEAD_DETAIL&quot;) & &quot;&quot;&quot;;&quot;
response.write &quot;}</script>&quot;

was a dynamic link and changed part of the page to have Head Detail from a table Dave

Theres nothing worse than an idea when its the only one we have!
 
Hmm what am i doing wrong here. I tried declareing the variable nam in the javascript and got this error.
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/logginBob1.asp, line 7
this is line 7
Code:
<script language=&quot;Javascript&quot; >
 (*line seven*) var nam1 = <%nam%>
	function ChkID(){
		ID=BobForm.UserID.value;
		if(ID=='' || ID != nam1){
		alert('You must enter a valid User Name');
   		event.returnValue=false;
		}
 
Try <%=nam%> instead of <%nam%>


mwa
 
var nam1 = &quot;<%nam%>&quot; -----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
oops now I'm not getting an error but it still doesn't work.
before I forgot the = now with it in no error but not working either. here's my last attempt.
Code:
<script language=&quot;Javascript&quot; >
 var nam1 = <%=nam%>
	function ChkID(){
		ID=BobForm.UserID.value;
		if(ID != nam1){
		alert('You must enter a valid User Name');
   		event.returnValue=false;
		}
this is the source code as it's printing to the page it's not printing the variable.
 var nam1 = 
	function ChkID(){
		ID=BobForm.UserID.value;
		if(ID != nam1){
		alert('You must enter a valid User Name');
   		event.returnValue=false;
		}
I really appreciate the help folks.
 
Hmm tried that and now it's printing
Code:
<script language=&quot;Javascript&quot; >
 var nam1 = &quot;&quot;;
	function ChkID(){
		ID=BobForm.UserID.value;
		if(ID != nam1){
		alert('You must enter a User Name');
   		event.returnValue=false;
		}
still won't print the variable from the asp code in the javascript code.
 
at the top of the page do
Response.Write nam

if the value is &quot;&quot; then nothing is being passed so it's time to start debugging and moving backwarss to find the source of the empty value I dare to learn more
admin@onpntwebdesigns.com
 
Here's the entire page code just in case there's something else I'm missing in regard to it not printing the variable nam. Please forgive the repetition of code this is my working copy.
Code:
<%Language=VBScript%>

<html>
<head>
<title> Password Checker</title>
<script language=&quot;Javascript&quot; >
 var nam1 = &quot;<%=nam%>&quot;;
	function ChkID(){
		ID=BobForm.UserID.value;
		if(ID != nam1){
		alert('You must enter a User Name');
   		event.returnValue=false;
		}

	
	ID=BobForm.PhArea.value;
       	RegX=/\d/;
     	if (RegX.test(ID)!=true || ID.length !=3)
	{
	alert(&quot;Please input a valid Phone number!&quot;);
	event.returnValue=false
	}
	//ID=BobForm.PhArea.value;
       //	RegX=/\d/;
     	///RegXA=(ID.search(RegX)!= -1);
	//if (RegXA!=true || ID.length !=3)
	//{
	//alert(&quot;Please input a valid Phone number!&quot;);
	//event.returnValue=false;
	//BobForm.MainNum.focus()
	//BobForm.MainNum.select()
	//}

	ID1=BobForm.PreNum.value;
       	RegX=/\d/;
     	 RegXA=(ID1.search(RegX)!= -1);
	if (RegXA!=true || ID1.length !=3)
	{
	alert(&quot;Please input a valid Phone number!&quot;);
	event.returnValue=false;
	BobForm.MainNum.focus()
	BobForm.MainNum.select()
	}

	ID2=BobForm.MainNum.value;
       	RegX=/\d/;
     	 RegXA=(ID2.search(RegX)!= -1);
	if (RegXA!=true || ID2.length !=4)
	{
	alert(&quot;Please input a valid Phone number!&quot;);
	event.returnValue=false;
	BobForm.MainNum.focus()
	BobForm.MainNum.select()
	}
		IDp=BobForm.phone.value;
       	RegXp=/\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
     	if (RegXp.test(IDp)!=true )
	{
	alert(&quot;Please input a valid Phone numbertest!&quot;);
	event.returnValue=false;
	}

	Ag=BobForm.age.value;
	if (Ag== '' || Ag <1 || Ag >101)
	{
	alert(&quot;Please input a valid Age&quot;);
	event.returnValue=false;
	BobForm.age.focus()
	BobForm.age.select()
	}
	
	
			YR=BobForm.year.value;
		if(YR==&quot; &quot;){
			alert('You must enter a Year');
   			event.returnValue=false;

			}
			Mth=BobForm.month.value;
		if(Mth==&quot; &quot;){
			alert('You must enter a Month');
   			event.returnValue=false;

			}
		Dy=BobForm.day.value;
		if(Dy==&quot; &quot;){
			alert('You must enter a Day');
   			event.returnValue=false;

			}

	str=BobForm.Email.value;
 	filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)!=true || str=='')
	{
	alert(&quot;Please input a valid email address!&quot;);
	event.returnValue=false;
	BobForm.Email.focus()
	BobForm.Email.select()
	}

	pass1=BobForm.Password.value;
	pass2=BobForm.Password2.value;
	if (pass1 != pass2 || pass1=='' || pass1.length <5){
	alert (&quot; You have entered invalid password or Entered passwords did not match &quot;)
	event.returnValue=false;
	BobForm.Password.focus()
	BobForm.Password.select()
	}

}

</script>
</head>
<body>


 <%

 Dim MyArray(4,1),pass,nam,i,test


 pass = request.form(&quot;Password&quot;)
 nam = request.form(&quot;UserID&quot;)

 MyArray(0,1) = &quot;john&quot;		
 MyArray(1,1) = &quot;mark&quot;
 MyArray(2,1) = &quot;jim&quot;
 MyArray(3,1) = &quot;doug&quot;
 MyArray(4,1) = &quot;tom&quot;

 MyArray(0,0) = &quot;john12&quot;		
 MyArray(1,0) = &quot;mark12&quot;
 MyArray(2,0) = &quot;jim123&quot;
 MyArray(3,0) = &quot;doug12&quot;
 MyArray(4,0) = &quot;thomas&quot;


	for i = 0 to 4						
	 If MyArray(i,1) + MyArray(i,0) =  nam + pass Then	
  	  test=true
          Exit For
	 End If
	next
	
	If test = true then
	  response.write &quot;You are successfull&quot;

	

%>

    <table align=&quot;center&quot; border=&quot;1&quot;><tr><td>
    <table style=&quot;background: #037CBB&quot; border=&quot;1&quot; align=&quot;center&quot;>	<!--Tables to hold form-->
      <form method=&quot;post&quot; action=&quot;254Bob.asp&quot; name=&quot;BobForm&quot; onSubmit=&quot;ChkID();&quot;>
        <tr> 
          <td  > 
            User Name
          </td>
          <td colspan = &quot;3&quot;> 
            <input type=&quot;text&quot; name=&quot;UserID&quot;>
         </td>
        </tr>
       <tr> 
          <td> 								<!--Form to accept users password and userID-->
            Password
          </td>
          <td colspan = &quot;3&quot;> 
            <input type=&quot;password&quot; name=&quot;Password&quot;>
          </td>
        </tr>
	<tr> 
          <td> 								<!--Form to accept users password and userID-->
            Confirm Password
          </td>
          <td colspan = &quot;3&quot;> 
            <input type=&quot;password&quot; name=&quot;Password2&quot;>
          </td>
        </tr>
	<tr> 
          <td> 								<!--Form to accept users password and userID-->
           Phone number
          </td>
          <td> 
            	<input type=&quot;text&quot; size = &quot;3&quot; name=&quot;PhArea&quot;>   
        	<input type=&quot;text&quot;size = &quot;3&quot; name=&quot;PreNum&quot;>
		<input type=&quot;text&quot; size = &quot;4&quot; name=&quot;MainNum&quot;>
	</td>
        </tr>
        <tr>
	 <td>Age</td>
	 <td><input type=&quot;text&quot; size = &quot;3&quot; name=&quot;age&quot;>
	 </td>
	</tr>
	        <tr>
	 <td>full phone</td>
	 <td><input type=&quot;text&quot; size = &quot;14&quot; name=&quot;phone&quot;> <!-- for entering full phone number--> 
	 </td>
	</tr>
	<tr>
          <td> 								<!--Form to accept users password and userID-->
          Birthdate
          </td>
          <td> 
            Year<select size = &quot;1&quot; name=&quot;year&quot;>
		<option selected value =&quot; &quot;>Year</option>
		<%
                  counter = 1945
                  Do while counter < 2001
	            Response.write &quot;<option value='&quot; & counter & &quot;'>&quot; & counter & &quot;</option>&quot;
                    counter = counter + 1
                  Loop
	 	%></select>
          </td>
       
	   <td> Month </td> 
		    <td><select name=&quot;month&quot; size=&quot;1&quot;>
    			<option selected Value = &quot; &quot;>Month</option>
    			<option value = &quot;January&quot;>Jan</option>
    			<option value = &quot;Febuary&quot;>Feb</option>
    			<option value = &quot;March&quot;>Mar</option>
    			<option value = &quot;April&quot;>Apr</option>
    			<option value = &quot;May&quot;>May</option>			
    			<option value = &quot;June&quot;>Jun</option>			<!--populate month drop down list-->
    			<option value = &quot;July&quot;>Jul</option>
    			<option value = &quot;August&quot;>Aug</option>
    			<option value = &quot;September&quot;>Sep</option>
    			<option value = &quot;October&quot;>Oct</option>
    			<option value = &quot;November&quot;>Nov</option>
    			<option value = &quot;December&quot;>Dec</option>
  		      </select></td>
	        <td> 
          Day  <select size = &quot;1&quot; name = &quot;day&quot;>
		<option selected value =&quot; &quot;>Day</option>                
		<%counter = 1
                  Do while counter < 32
	            Response.write &quot;<option value='&quot; & counter & &quot;'>&quot; & counter & &quot;</option>&quot; '# populate options for day input
                    counter = counter + 1
		  Loop%>
		</select>
	</td>
</tr><tr>
</tr><tr><td>Email</td>
<td><input type=&quot;text&quot;  name=&quot;Email&quot;>
</td>
</tr><tr>
          <td style=&quot;text-align: right;&quot;> 
            <input type=&quot;reset&quot; value=&quot;Reset&quot;><input type=&quot;submit&quot; value=&quot;Submit&quot;>
          </td>
        </tr>
      </form>
    </table>
    </td></tr></table>
	 <%
	Else
 	 response.write &quot;You're unsuccessfull please try again&quot;
	 response.addheader&quot;Refresh&quot;,&quot;2;URL= logginBob1.htm&quot;
	end if
	%>
</body>
</html>
Everything works but the username validation.
 
Nam is being passed as it works for the password/username validation. It's just not being printed in the Javascript code.
 
I'm not sure if it is effected, but you are using the asp variable &quot;nam&quot;
var nam1 = &quot;<%=nam%>&quot;;
long before you are putting a value in it:
nam = request.form(&quot;UserID&quot;)

Since the server will process all asp rows top down, I think that you need to make sure to load the variables before you use them (move the asp processing before the HTML page) -----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
Another thing, it appears that you are attempting to compare the value they entered before submitting the form to the value they have entered on this page...
Code:
ID=BobForm.UserID.value; //value of userID on this page
if(ID != nam1){  //if value on this page is not equal to value on previous page?
  alert('You must enter a User Name');
  event.returnValue=false;
}

Am I missing something here? It appears you have a server-side evaluation of the userId which can be passed, then a client-side evaluation that can only be passed if they re-enter their user ID and submit again (the first form will also validate client-side if the UserId is blank)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Ahh thanks a million mwolf00 that worked!!! I just had to move the request.form code above the javascript code. You definitly get a star from me.
 
Before you have <%Language=VBScript%> at the top try

<%
response.write &quot;<script LANGUAGE=&quot;&quot;JavaScript&quot;&quot;>nam1 = &quot;&quot;&quot; & nam & &quot;&quot;&quot;</script>&quot;
%>

This should define the variable in javascript and insert the value of nam into it.

My last post was unnecessarily coded but i know to pass the variable through to javascript you cannot call it via <%=name%> within the scipt tags.

Try it. (just delete your 'var nam1 = &quot;<%=nam%>&quot;;' in your javascript) Dave

Theres nothing worse than an idea when its the only one we have!
 
Try it. (just delete your 'var nam1 = &quot;<%=nam%>&quot;;' in your javascript)

This is unnecesary, the following two statements are identical:
Code:
<% Response.Write &quot;nam1 = '&quot;&nam&&quot;';&quot; %>
nam1 = '<%=nam%>';

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
K Tarwin let me try explain. I don't want the user to be able to change his username and I want to pass the username to another asp page. (it's a long story) . I could've used a number of other options like a hidden form element with the username printed inside it or a session variable,cookie,querystring ect. As I'm just playing with the code right now I wanted to be able to pass an ASP variable to a javascript variable and didn't know if it could be done. Now I know and really appreciate all your guys help. I'm just a rookie but and I guess I have a little too much time on my hands right now. I do want to reiterate though that this forum and you guys are the greatest! I really do appreciate all your help!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top