Troy Cosentino
Troy Cosentino

Reputation: 4778

What does an empty input's value when using POST in asp?

I am using asp (JScript as my language) and working with getting data from a form that is sent using POST.

Specifically, I have a text input and I want to check if it was left empty. When leaving it empty, and including Response.Write(Request.form('opt2Dur')) in the called page, nothing prints (doesn't print null or undefined).

My thought was that it was just an empty string so I included this: Response.Write(Request.form('opt2Dur') === ''), however this printed false.

It will print true if I use Response.Write(Request.form('opt2Dur') == '') (== not ===). What is the true value that I can check against using ===? Or, in this case will it be sufficient to check with just ==?

Thanks for any help.

Upvotes: 1

Views: 949

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66388

In scripting languages with "flexible" types and default values it's very easy to get confused with actual data types.

The actual type of each Request item (both QueryString and Form, it doesn't matter) is some sort of Array as it also supports more than one form element with the same name submitted to the ASP handler. It's mentioned in the documentation as well:

The Form collection is indexed by the names of the parameters in the request body. The value of Request.Form(element) is an array of all the values of element that occur in the request body.

Since the === also take into account type, it will return false in your case as array is not a string.

I wasn't able to find the exact actual type and reproduce it with local variable (it's not any plain array) so if you are keen on using the strict comparison operator, check the Count:

Response.Write(Request.Form('opt2Dur').Count === 0);

Upvotes: 1

Related Questions