...

View Full Version : cant test undefined QueryString (small test included)



arianhojat
08-23-2005, 09:28 PM
Hello all,
I had a lil question i hope one of you can answer...
i have a textfield that when you first goto the page, since the query string isnt made yet (value undefined), it fills in a default value like 0.

now i try testing for undefined which is what its says it is yet it tests true for != undefined, even though again it outputs undefined.... any ideas guys?

thanx in advance...


<%@LANGUAGE="JAVASCRIPT"%>

<html>
<head>
<title>scheduler</title>
</head>
<body>


<form>

Should be 0 when 1st goto page but get '1.#QNAN' instead...
<input type="text" name="attendeesNum" size="20" value="<% if( Request.QueryString("attendeesNum")!="undefined" ){ Response.write(parseInt(Request.QueryString("attendeesNum"))) }else{ Response.write("0"); } %>"/>
<br/>
<%

//
Response.Write( "QueryString value should output undefined=... <i>"+ Request.QueryString("attendeesNum") +"</i> Good"+"\n");

//Response.Write( "<br><b>"+(Request.QueryString("attendeesNum")!= "") +"</b>"+"\n");
//Response.Write( "<br><b>"+(Request.QueryString("attendeesNum")!= null) +"</b>"+"\n");

Response.Write( "<br>Is QueryString value whenn tested in if statement undefined though? (should be TRUE)<b>"+(Request.QueryString("attendeesNum")== "undefined" ) +"</b>"+"\n");

//*/
%>

<br/><br/>
<input type="submit" value="submit" />

</form>

</body>
</html>

Bullschmidt
08-23-2005, 11:15 PM
It's a zero length string (i.e. ""), and you can test with this on a page and use no querystring when opening the page and you should see the text, "It's a zero length string":



<%
If Request.QueryString("var1") = "" Then
Response.Write "It's a zero length string"
End If
%>

neocool00
08-24-2005, 04:00 PM
This might help you.
http://www.devguru.com/technologies/javascript/11465.asp

pet = "Rachel has 312 baby aardvarks"
CheckNum = parseInt(pet)
if(isNaN(CheckNum))
{
document.write("<BR>Sorry, CheckNum is a NaN")
document.write("<BR>Left-most character = " + pet.substring(0,1))
}

OUTPUT:
Sorry, CheckNum is a NaN
Left-most character = R
Explanation: You can use the isNaN function to see if the returned value is a NaN.
*NaN = Not a Number

arianhojat
08-24-2005, 08:58 PM
ahhh yay! parseInt and NaN solution worked. The check for a 0 length string did not work unless u posted, not when u arrive to the page which is what i wanted, not sure why.
but thanx again.

neocool00
08-24-2005, 10:08 PM
I don't think you can check for "undefined" state in Javascript, it's more of an error message. Although it seems to me that you could have done this as well.

="<% if( Request.QueryString("attendeesNum")!="" ){ Response.write(parseInt(Request.QueryString("attendeesNum"))) }else{ Response.write("0"); } %>"
That should check for an empty string (as in the first time you hit the page). Not sure though, I don't use server side Javascript only client-side.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum