jeudi 21 avril 2011
ASP: is this a date? The IsDate function
Do you like this story?
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.
While complete JavaScript form validation solutions are available everywhere, today I would like to point out that VBScript has a very simple function that can help us in checking dates at least.
The function
The 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 examples
IsDate syntax is very basic:IsDate(Expression)Let's see an elementary example:
<% 
Dim test_date
test_date = "22/04/2011"
response.write (IsDate(test_date))
%>True<%
Dim test_date
test_date = "Hello!"
response.write (IsDate(test_date))
%>FalseWhy "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.<%
Dim input_date
input_date = Request.Form("submitted_date")
if IsDate(input_date) = "True" then
   your submitting to database code
else
   response.write("Please, insert a valid date")
end if
%>Enjoy, and let me know what you think about it in the comments section.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “ASP: is this a date? The IsDate function”
Enregistrer un commentaire