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

HELP if test (seemingly) taking wrong branch

Status
Not open for further replies.

toddj1

Programmer
Nov 6, 2002
3
US
Hello
I am running a very simple javascript as an ASP on a windows 2000 server! (KEYNOTE javascript on an ASP windows 2000 server!!!!) Its my understanding as long as you start your script with &quot;<%@ language=&quot;javascript&quot;%>&quot; it will work! Apparently it does (well almost) because without it you get all kinds of syntax errors.

In this code I have a form with 2 fields f1 & f2 plus a submit button. Pressing the submit button runs the same script to process f1 & f2.

The problem is an if test I have that compares f1 & f2. It reads &quot;if (f1 != f2) { ....&quot;

No matter what values are put in f1 & f2 it always enters the &quot;then part of the if&quot;, even if f1 & f2 are equal (say 1 & 1)! Weird and probably a DUH but I just can't see it!

As a side test I changed the if to a less than (<) or greater than (>) test and entered numbers &quot;accordingly&quot;! The if test worked in that state! Therefore the &quot;if test&quot; is apparently working and so is javascript just not for != or == constructs! (yup tried == too!)

Again you must have access to a windows 2000 ASP engine to test this! Just so you know, This has already stumpped another forum and my Hosting service! In fact there words were &quot;The code appears to be fine and several of us pulled our hair out about it earlier today and still couldn`t see it.&quot;

Anyway Thanx in advance!



Here is ASP code.
<%@ language=&quot;javascript&quot;%>
<%
// Are we processing the form else posting the form!
if (Request(&quot;Func&quot;) == &quot;Process Form&quot;) {
Response.Write(&quot;Processing Form&quot;);
f1 = Request(&quot;f1&quot;);
f2 = Request(&quot;f2&quot;);
Response.Write(&quot;f1=(&quot;+f1+&quot;)f2=(&quot;+f2+&quot;)&quot;);
if (f1 != f2){
Response.Write(&quot;Sorry, f1 & f2 are not identical!&quot;);
Response.Write(&quot;Notice the print above. If they are identical, then why doesn't this work? &quot;);
}
Response.Write(&quot;Use the Back button to try again&quot;);
Response.End();

}
else {
%>
<html>
<head>
<title>Form Tester</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<body bgcolor=&quot;#FFFFFF&quot; leftmargin=&quot;0&quot; topmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot;>

<form method=&quot;post&quot; action=&quot;<%=Request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; name=&quot;theform&quot;>
f1: <input type=&quot;text&quot; name=&quot;f1&quot;>
f2: <input type=&quot;text&quot; name=&quot;f2&quot;>
<input type=&quot;submit&quot; name=&quot;Func&quot; value=&quot;Process Form&quot;>
</form>

</body>
</html>
<%
}
%>
 
this line if (f1 != f2) is partialy correct. there needs to be more though. the != (not equal operator) returns a Boolean value true or false, so simple stating f1 != f2 isn't enough due to all this doing is returning true of false. so a easy fix is to do something along the lines of this
if((f1 != f2) == true){
now if the conditional is met to being true you can perform the next task.

try this example out to see what I mean. change the values around a bit to play with it and get the idea of how this operator works
this is client side
<script>
function test() {
var f1 = &quot;2&quot;;
var f2 = &quot;1&quot;;
if((f1 != f2) == true){
alert(&quot;not equal&quot;);
}
else
{
alert(&quot;equal&quot;);
}
}
</script>


also as to your first question you are correct. Yes you need to indication of the javascript being used. asp defaults to vbscript otherwise. A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
I'm not exaclty understanding your point. If (f1 != f2) returns true or false, isnt' that all that is needed for an if then logic block?


I can't remember Javascript so well but in Java this will always evaluate to false because you are comparing objects and not the contents of the object. Does something like
Code:
if (f1.toString() != f2.toString())
{

}
else
{
}
work in Javascript?
Thanks,

Gabe
 
your right that doesn't make any difference. I had a strang one once though that I needed to specify the return value. I believe in that case it was false though. thoguht I'd give it a try here and see if it helped out A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Making the following changes will work:
Code:
   var f1 = new String(Request(&quot;f1&quot;)); 
   var f2 = new String(Request(&quot;f2&quot;)); 
   Response.Write(&quot;f1=(&quot;+f1+&quot;)f2=(&quot;+f2+&quot;)<br>&quot;); 
   if (f1.toString() != f2.toString()){

The reason you could do equivalence tests was because 1) it didn't know what kind of objects it had, 2) it didn't know what value it was testing equivalence on and was probably using memory reference value (ie the names weren't referencing the same objects). 3) By declaring them as strings they inherit the attributes of strings like knwoing exactly what to output for toString().

Hope this helps.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thanx Guys

I selected Tarwn's code. Onpnt your code worked in my simple test that I used here but when I copied and pasted it to the real thing, for some reason I got the same results! Weird. Oh well got it going now!


Thanx a bunch!

Toddj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top