Thursday, May 10, 2012

server side date validation Asp


There may be situation when u need to do Server Side Date Validation

Asp server side validation
Data needs to be validate at server side because client side can be stop by disabling javascript.
server side validation is validation which get executed when page is posted
on server. But it is good practice to use both server side and client side
valition in Application.
Below example shows how to do Server Side Date Validation

1) Copy the below code in text file and save it as ServersideDatevalidation.asp

<%
Function ValidDate(DateFormat)
Set re = New RegExp
re.Pattern = "^(([0-9])|([0-2][0-9])|([3][0-1]))[-](([0-9])|([0-0][0-9])|([0-1][0-2]))[-]\d{4}quot;
If not re.Test(DateFormat) Then
ErrorMsg="Invalid Date"
End If
ValidDate=ErrorMsg
End Function

Function CompareDate(FromDate1,ToDate1)
Dim arrfr,arrto
arrfr = split(FromDate1,"-")
FromDate1=arrfr(1)&"/"&arrfr(0)&"/"&arrfr(2)
arrto=split(ToDate1,"-")
ToDate1=arrto(1)&"/"&arrto(0)&"/"&arrto(2)
FromDate1=cDATE(FromDate1)
ToDate1 =cDATE(ToDate1)
if ToDate1 <> "" and FromDate1 <> "" then
if datediff("d",date(),FromDate1) > 0 then
ErrorMsg="From date should not be future date"
elseif datediff("d",date(),ToDate1) > 0 then
ErrorMsg="To date should not be future date"
elseif datediff("d",ToDate1,FromDate1) > 0 then
ErrorMsg="From date should not be greater than To date"
end if
else
ErrorMsg= "To Date and/or From date should not be blank"
end if
CompareDate=ErrorMsg
End Function
%>

2) now u can include the above file and use it the below mention manner..
<%
if Request.ServerVariables("HTTP_METHOD") = "POST" THEN
StrErrDis = ""
StrErrDis= ValidDate(Request.Form("txtfromopen"))
StrErrDis = StrErrDis & ValidDate(Request.Form("txttoopen"))

if StrErrDis = "" then
StrErrDis=CompareDate(Request.Form("txtfromopen"),Request.Form("txttoopen"))
end if

If StrErrDis <> "" Then
txtErr_message = StrErrDis
Response.Write StrErrDis
Response.End
End If

END IF
%>
GooglePlus

No comments:

Post a Comment