Posted by on in iDevelop | 0 comments

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.

  1. username = Request.Form("username")
  2. password = Request.Form("password")
  4. If username = "" Then
  5.     Response.Write("enter your username")

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:

  1. username = Trim(Request.Form("username"))
  2. password = Trim(Request.Form("password"))
  4. If username = "" Then
  5.     Response.Write("enter your username")

this would’ve been quite better!!
But IMHO a better solution is one of Marcus’ handy functions, IsBlank().

  1. Function IsBlank(ByRef Var)
  2.     IsBlank = False
  3.     Select Case True
  4.         Case IsObject(Var)
  5.             If var Is Nothing Then
  6.                 IsBlank = True
  7.             End If
  8.         Case IsEmpty(Var), IsNull(Var)
  9.             IsBlank = True
  10.         Case IsArray(Var)
  11.             If UBound(Var) = 0 Then
  12.                 IsBlank = True
  13.             End If
  14.         Case IsNumeric(Var)
  15.             If (Var = 0) Then
  16.                 IsBlank = True
  17.             End If
  18.         Case Else
  19.             If Trim(Var) = "" Then
  20.                 IsBlank = True
  21.             End If
  22.     End Select
  23. End Function

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

  1. Public Function IsBlank(ByRef TempVar) As Boolean
  2.     Dim tmpType
  3.     'by default, assume it's not blank
  4.     IsBlank = False
  5.     'now check by variable type
  6.     Select Case VarType(TempVar)
  7.         'Empty & Null
  8.         Case 0, 1
  9.             IsBlank = True
  10.         'String
  11.         Case 8
  12.             If Len(TempVar) = 0 Then
  13.                 IsBlank = True
  14.             End If
  15.         'Object
  16.         Case 9
  17.             tmpType = TypeName(TempVar)
  18.             If (tmpType = "Nothing") Or (tmpType = "Empty") Then
  19.                 IsBlank = True
  20.             End If
  21.         'Array
  22.         Case 8192, 8204, 8209
  23.             'does it have at least one element?
  24.             If UBound(TempVar) = 0 Then
  25.                 IsBlank = True
  26.             End If
  27.     End Select
  28. End Function

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

  1. username = Trim(Request.Form("username"))
  2. password = Trim(Request.Form("password"))
  4. If IsBlank(username)=True Then
  5.     Response.Write("enter your username")

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

  1. $username = trim($_POST['username']);
  2. $password = trim($_POST['password']);
  4. if(empty($username)) {
  5.     print("enter your username");

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!! 😉