If you take some kind of input from a user in an application(whether its online or offline, it hardly matters) and then perform some action using that input, then you must check if the input is empty or not before proceeding with the action, its common programming sense, unless ofcourse you also allow NULL values.
Now there are different ways of checking whether the input is null or not, in different programming languages, but since ASP(with VBScript) is years old & now deprecated, its library is not as rich as others today, so it doesn’t quite have what others have. But it doesn’t mean that it can’t have what it doesn’t have. I’ve not coded in ASP for more than an year now, but I do see some code now & then & it doesn’t surprise me that people still validate the user input in such an idiotic way that you’ll wonder if they are really any good!! 🙄
Take the following example for instance, its an excerpt from a code where some dude is getting some data from a form submitted by user & validating it in ASP/VBScript.
[asp]
username = Request.Form(“username”)
password = Request.Form(“password”)
If username = “” Then
Response.Write(“enter your username”)
[/asp]
Now the idiot isn’t even trimming the spaces around the value to make it somewhat good enough to be caught by the IF statement. All one has to do is put in 1 space char in both the ‘username’ & ‘password’ fields & the ASP script will do the expensive database check to see if they match!! Ofcourse nothing will turn up & the script will say so to the user, but then that could’ve been done without making that expensive database call. If nothing else, he could atleast have done this:
[asp]
username = Trim(Request.Form(“username”))
password = Trim(Request.Form(“password”))
If username = “” Then
Response.Write(“enter your username”)
[/asp]
this would’ve been quite better!!
But IMHO a better solution is one of Marcus’ handy functions, IsBlank().
[asp]
Function IsBlank(ByRef Var)
IsBlank = False
Select Case True
Case IsObject(Var)
If var Is Nothing Then
IsBlank = True
End If
Case IsEmpty(Var), IsNull(Var)
IsBlank = True
Case IsArray(Var)
If UBound(Var) = 0 Then
IsBlank = True
End If
Case IsNumeric(Var)
If (Var = 0) Then
IsBlank = True
End If
Case Else
If Trim(Var) = “” Then
IsBlank = True
End If
End Select
End Function
[/asp]
This function is quite cool & you can use it not only in ASP with VBScript, but also in VB6 if you want, by adding some declarations as VB requires them. So I was able to get the following to work in VB6
[vb]
Public Function IsBlank(ByRef TempVar) As Boolean
Dim tmpType
‘by default, assume it’s not blank
IsBlank = False
‘now check by variable type
Select Case VarType(TempVar)
‘Empty & Null
Case 0, 1
IsBlank = True
‘String
Case 8
If Len(TempVar) = 0 Then
IsBlank = True
End If
‘Object
Case 9
tmpType = TypeName(TempVar)
If (tmpType = “Nothing”) Or (tmpType = “Empty”) Then
IsBlank = True
End If
‘Array
Case 8192, 8204, 8209
‘does it have at least one element?
If UBound(TempVar) = 0 Then
IsBlank = True
End If
End Select
End Function
[/vb]
Not a big deal, I know, but its quite handy. You can use this VB6 version as it is in ASP(VBScript) as well, it’ll work!! So that sloppy code mentioned earlier, using this function, will become
[asp]
username = Trim(Request.Form(“username”))
password = Trim(Request.Form(“password”))
If IsBlank(username)=True Then
Response.Write(“enter your username”)
[/asp]
This is indeed much better, and since this IsBlank function can also check on Objects etc. its indeed quite handy!! 😉
Since PHP has quite a good function empty(), it doesn’t need a port of IsBlank. In PHP, this code would be a breeze
[php]
$username = trim($_POST[‘username’]);
$password = trim($_POST[‘password’]);
if(empty($username)) {
print(“enter your username”);
[/php]
So I just wish that you won’t do mistakes like the one mentioned here, which is, not properly checking whether the input you received is empty or not!! 😉