The Val() function may or may not work for you in VBScript. In any case, it has some dangers as it can fail with a Type Mismatch error in some situations where it should not.
CInt() doesn't work if the string you're trying to work has non-numeric characters in it.
Some people write their own Val() function to step through a string character by character and pull out the numbers. I've also seen solutions where people used regular expressions to remove non-number characters and then throw CInt() at the number.
But an alternate way is to simply use server-side javascript:
This does not go inside the usual <% %> enclosing marks.
This method is probably faster than a custom function, and can handle more expressions than simply removing non-number characters.
Now in your ASPs you can simply replace val with one of these functions like so!
You can use this method of calling server-side javascript in any situation where you have a function that you like from javascript. For example, if you validate some controls on a form in the client's browser using javascript, why write it in VBScript all over again? Just copy the exact function to the server and validate it. This makes development faster and more robust.
I have tested the Int function but not the Float function.
Erik
PS It's considered good practice to write client-side script in javascript. This ensures compatibility with non-Microsoft browsers, and helps separate server script from client script in your mind. Of course, here I am telling you to break that rule. But sometimes it makes sense.
CInt() doesn't work if the string you're trying to work has non-numeric characters in it.
Some people write their own Val() function to step through a string character by character and pull out the numbers. I've also seen solutions where people used regular expressions to remove non-number characters and then throw CInt() at the number.
But an alternate way is to simply use server-side javascript:
Code:
<SCRIPT language="javascript" runat="server">
function VBValInt(theexpression) {
var thevalue = parseInt(theexpression)
if (isNaN(thevalue)) thevalue = 0
return thevalue
}
function VBValFloat(theexpression) {
var thevalue = parseFloat(theexpression)
if (isNaN(thevalue)) thevalue = 0
return thevalue
}
</SCRIPT>
This method is probably faster than a custom function, and can handle more expressions than simply removing non-number characters.
Now in your ASPs you can simply replace val with one of these functions like so!
Code:
Response.Write VBValInt(Request.QueryString("value"))
I have tested the Int function but not the Float function.
Erik
PS It's considered good practice to write client-side script in javascript. This ensures compatibility with non-Microsoft browsers, and helps separate server script from client script in your mind. Of course, here I am telling you to break that rule. But sometimes it makes sense.