PDA

View Full Version : ASP IF nightmare


TheWolf1
04-17-2009, 09:38 PM
Hello!

I'm a total newb with ASP and VBScript, but I'm versed in PHP.

What I'm trying to do is to validate POST data.

In PHP it's soo simple:
$aQty = array_key_exists('qty', $_POST) && is_numeric($POST['qty']) && $POST['qty'] > 0 ? $POST['qty'] : 0;

I check if the value exists, if it does, it must be numeric and greater than 0. If anything is wrong it sets the var to 0 othervise to the post data.

How can I do this in ASP!?? ASP seems to use lazy evaluation which is a nightmare for me!

Please save the remaining of my sanity!

Old Pedant
04-17-2009, 10:09 PM
Well, since you added that "if anything is wrong..." clause, it's really easy:

<%
...
qty = 0
On Error Resume Next
qty = CDBL( Request("qty") )
On Error GoTo 0
If qty < 0 Then qty = 0
...

Note: You *said* "must be numeric", which means that "0.0000001" and "123456789.0123456" and even "99E99" qualify. (And that appears to be all your PHP code is checking for, too.)

If you meant "integer and less than 2 billion or so" then change CDBL to CLNG.

If you meant "integer" and less than 32768 then change CDBL to CINT.

If you meant something more specific than that, ask.

TheWolf1
04-20-2009, 01:19 PM
Can you explain the following 2 lines:
On Error Resume Next
On Error GoTo 0

I guess it's the equivalent of try catch statement... If the CDBL cast fail, it continue execution right?

Old Pedant
04-20-2009, 07:42 PM
It's nowhere near as good as Try...Catch, but it's all there is in VBScript.

The statements are from ancient history, shades of the 1970s and 1980s.

On Error Resume Next means "if the runtime encounters an error it should stop executing the current line immediately and then just resume execution on the next line."

So if you do

qty = 0
On Error Resume Next
qty = CInt( Request("quantity") )
...

Then if anything goes wrong trying to perform Request("quantity") *OR* trying to take the CINT( ) of that value, no attempt is made at all to complete the assignment to the variable, and so it retains the pre-On Error value of zero.

On Error GoTo 0 is even more obscure. It used to be that lines in BASIC programs were numbered. And you could say GOTO 37 to transfer execution to line 37, for example. But you could *not* give the line number 0 to a line. So if you said "GOTO 0", the in itself was an error. So some bright boy got the idea of using that as the way to "turn off" the effects of ON ERROR.

Note that ON ERROR used to be a lot more flexible: You used to be able to do things like ON ERROR GOTO 1000 which meant "when you get an error stop immediately and transfer to line number 1000" or maybe ON ERROR GOSUB 2000 which meant "when you get an error, call the SUBROUTINE that starts at line 2000. And more, depending on the dialect of BASIC in use. Somehow, VBScript kept the skeleton of ON ERROR without keeping its flexibility so we are left with this inadequate excuse for error handling.

TheWolf1
04-21-2009, 04:53 PM
Wow I didn't knew ASP was so "weak"... Thanks for the explanation! Hope to never have to modify some ASP scripts again in the future!