Form validation is probably the most wanted feature by web developers. Every web programmer, at least once in a lifetime, has dealt with the issue: the user fills in the form and submit wrong data. Without validation, the best thing could happen is that an error is thrown. The worst case? The wrong data is submitted to your database... and believe me, most of the time you don't want that.
The functionThe function I am talking about is IsDate. I've already talked about the function in a post about SQL Server queries and date functions. The VBScript function works in a similar way.
Basically it returns a Boolean value (true/false) stating if an expression is a date or can be converted to a date. The function uses the local settings in the evaluation process, so be careful in using it: for example, English months in words are not the same in Italian.
The syntax and some examplesIsDate syntax is very basic:
Not much to say here. Expression is what we want to evaluate.
Let's see an elementary example:
The above snippet will output:
test_date = "22/04/2011"
test_date = "Hello!"
Quite easy, isn't it?.
Why "Form validation"?Well, it is quite clear that if we create an appropriate VBScript function, we can actually check form submission before inserting/updating data in our database.
The submitted_date is the date inserted into a form input field.
input_date = Request.Form("submitted_date")
if IsDate(input_date) = "True" then
your submitting to database code
response.write("Please, insert a valid date")
Enjoy, and let me know what you think about it in the comments section.