Date Validation using ASP.NET Validator

Go To


I am trying to validate if the date is valid meaning that its not in the future and also make sure its a legit date i.e 03/32/2012 is not valid. What I currently have works only to verify if the date is valid but it does not work when I place the date in future. Also I am having a problem if someone enters a date like 03/32/2012 it will say that its not valid but they can still click save button and then the exception will occur. What is the best way to approach this.

Here is my code:

<asp:TextBox ID="tbDate" runat="server" Text=""></asp:TextBox>
<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/App_Themes/Sugar2006/images/Calendar_scheduleHS.png" ImageAlign="Middle" />
<asp:CalendarExtender ID="ce" runat="server" TargetControlID ="tbDate" PopupButtonID="imgCalendar" />        
<asp:MaskedEditExtender ID="mex" runat="server" 
                        OnInvalidCssClass="MaskedEditError" />
<asp:MaskedEditValidator ID="mev" runat="server" 
                        InvalidValueMessage="This date is invalid!" Font-Bold="True" 
                        ForeColor="#D50000" />
<asp:RequiredFieldValidator runat="server" ID="DReq"
                        ErrorMessage="A Date is required." ValidationGroup="vgMyGroup"/>
<asp:ValidatorCalloutExtender runat="Server" ID="DReqE"
                        HighlightCssClass="validatorCalloutHighlight" />
2012-04-04 20:06
by Nick Manojlovic


I have set the ValidationGroup on the MaskedEditValidator and that fixed my issue. Also to fix to not allow future dates I have done the following inside the .cs file. All my issues have been resolved. Thanks. :)

    protected void mev_Init(object sender, EventArgs e) {
        ((MaskedEditValidator)sender).MaximumValue = DateTime.Now.ToString("MM/dd/yyyy");
2012-04-06 13:41
by Nick Manojlovic


You can also use the CustomValidator and point ClientValidationFunction to a custom javascript function.

Such a function would look like±

function validateField(source, args){ args.IsValid = true; }

In javascript you can use the following to check if a date is valid±

var timestamp = Date.parse(textBoxValue)

if (isNaN(timestamp)==false)
    var d = new Date(timestamp);


You can then compare d to the current date to check if it is in the future.

2012-04-04 20:17
by Wouter de Kort
Hey thanks for reply. I have added that code you suggested but then I still don't get notified of error - Nick Manojlovic 2012-04-04 20:45
Can you update your question with your new code - Wouter de Kort 2012-04-04 20:49
I have added a ValidationGroup="vgMyGroup" to the MaskedEditValidator which will not let the user commit unless the date is correct and that works... Now I just have to get it working so the user cannot enter a future date and if they do to also give error. There is a parameter on the MaskEditExtender to call ClientValidationFunction but it does not seem like its even hitting the javascript.. - Nick Manojlovic 2012-04-05 13:12
I have also placed MaximumValue='<%#DateTime.Now%>' and that works but does not get validated by the button click which is interesting - Nick Manojlovic 2012-04-05 14:01