views:

1216

answers:

1

I need a client-side date validator on a text box.

BAM - Custom Validator. Operator="DataTypeCheck", Type="Date". Boo-ya.

Golden, except that all dates get input in the format dd-MMM-yy. This method works fine for "12/12/2009" but fails on "12-Dec-09".

So, how do I get client side validation of dates in whatever format I choose to enter them?

Culture is currently en-AU if that helps at all.

Any ideas appreciated, cheers.

Update

OK I've now got this:

<asp:TextBox ID="txtDate" Columns="10" runat="server"></asp:TextBox>    
<asp:MaskedEditExtender ID="txtDate_MaskedEditExtender" runat="server"
        CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="$" 
        CultureDateFormat="ddMMMyy" Mask="99-LLL-99" CultureDatePlaceholder="-" CultureDecimalPlaceholder="." 
        CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" ClearMaskOnLostFocus="false"  
        TargetControlID="txtDate">
    </asp:MaskedEditExtender>

    <asp:CalendarExtender ID="txtDate_CalendarExtender" PopupPosition="TopLeft" runat="server" Enabled="True" TargetControlID="txtDate" Animated="false" Format="dd-MMM-yy"></asp:CalendarExtender>

    <asp:MaskedEditValidator ID="MaskedEditValidator1" runat="server" 
        ControlExtender="txtDate_MaskedEditExtender" ControlToValidate="txtDate"  
        ErrorMessage="Invalid Date" Display="Dynamic" Text="*" IsValidEmpty="true" EmptyValueMessage="*" EmptyValueBlurredText="*">*</asp:MaskedEditValidator>

But my masked edit validator is doing nothing.

Any ideas ?

A: 

There are many input controls that enforce a 'mask' that, on each keypress, disables any input character that doesn't match the mask.

Have a look at the the Microsoft ASP.Net AJAX Control Toolkit for an example of one.

You can then enter a date format string as the mask.

On a quick glance I can't see a way to be flexible in allowing the two different formats you require but the control does make it pretty clear what format is required. I'd recommend you need to play with it to see what's possible. The ability to easily supply a pop-up calendar may be helpful too.

rohancragg