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

If Len(iOptionID)<1 OR IsNumeric(iOptionID)...........

Status
Not open for further replies.

Cullen411

Programmer
Aug 17, 2005
89
0
0
GB
I want to make sure that the querystring value is numeric, and that it isn't empty otherwise do a redirect
The code below works

iOptionID = Request.QueryString("ID")
If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False Then
Response.redirect "admin_options.asp"
End If

How could I make sure that iOptionID is greater than 0?

iOptionID = Request.QueryString("ID")
If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False OR iOption<1 Then
Response.redirect "admin_options.asp"
End If

I tried the code above and it doesn't work when the id=
I get the error

Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: ""]'

any ideas?
 
>I want to make sure that the querystring value is numeric, and that it isn't empty otherwise do a redirect
>[tt]If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False OR iOption<1 Then[/tt]

[tt]If Len(iOptionID)=0 OR not IsNumeric(iOptionID) Then
Response.redirect "admin_options.asp"
End If
[/tt]
 
here's what I asked you've just given me the same code I basically already had.



How could I make sure that iOptionID is greater than 0?

iOptionID = Request.QueryString("ID")
If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False OR iOption<1 Then
Response.redirect "admin_options.asp"
End If

I tried the code above and it doesn't work when the id=
I get the error

Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: ""]'

any ideas?
 
this one:

If iOptionID<>"" Then
If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False OR iOption<1 Then
Response.redirect "admin_options.asp"
End If
End if

-DNG
 
Unlike the C programming language, Visual Basic and its variants evaluate ALL expressions in an IF/THEN conditional.

Suppose in C you have this:
[tt]if (iOptionID == "" || IsNumeric(iOptionID) == false)[/tt]

Now if iOption is indeed equal to "" then it stops right there without evaluating the IsNumeric() method because no matter if it returns true or false the overall statement is true because:
True or True = True
True or False = True


Visual Basic, on the other hand, will evaluate every bit of the expression, even though the "or" expression MUST be true because the first part is true.

The implication of all this is that you can't use anything in a compound conditional evaluation that will throw an error based on a possible value... you need to break that into two separate conditions.

A more obvious example:
[tt]IF (Not IsNumeric(X) OR (X + 5 < 10)) THEN[/tt]

So if X is indeed not a number then Not IsNumeric is true... and therefore the OR condition is also treu.... but VB continues along and evaluates X+5 which will raise a type-mismatch error.
 
Oh, and as for the reason... the explanation I've heard is that Visual Basic uses the same equal sign syntax for an assignment as for an eqivilancy test.

So in VB you do an assignment like this:
X = 5

And you test eqivilancy like this:
if x = 5 then

But you can also do an assignment of equivilancy like this:
bSame = 5 = 10
... that leaves the value of bSame as the boolean True

So the excuse is that VB needs to figure out the meaning by context and that it is necessary to evaluate the entire expression to get the proper context.

C and its syntacitcal variants (C++, Java, JavaScript, etc..) use a single equal sign for assingments and a double equal sign for equivilancy.
x = 5
if (x == 5)
x = 5 == 10
 
Try adding an
not isNull(iOptionID)

in your if statement.
 

Code:
dim bInvalid: bInvalid = false
dim iOptionID: iOptionID = trim(Request.QueryString("ID"))

If Len(iOptionID)<1 OR Not(IsNumeric(iOptionID)then
  bInvalid = true
else
 if iOption < 1 Then
   bInvalid =true 
 End If
end if

if bInvalid then
  Response.redirect "your_invalid_page.asp"
end if


A smile is worth a thousand kind words. So smile, it's easy! :)
 
>I want to make sure that the querystring value is numeric, and that it isn't empty otherwise do a redirect
>If Len(iOptionID)<1 OR IsNumeric(iOptionID)=False OR iOption<1 Then
>here's what I asked you've just given me the same code I basically already had.
>How could I make sure that iOptionID is greater than 0?

[1] Now, you tell the forum what is iOption.
[2] You give me an example of numeric in string format which is negative and have length less than 1.
 
Give this a try
Code:
	if (iOptionID = "" or isNull(iOptionID)) and (Len(iOptionID) = 0 or not IsNumeric(iOptionID)) then
		Redirect
	else
		Do your code
	End if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top