Any ideas why this MaskedEditValidator isn't behaving properly?

1.7k Views Asked by At

I am trying get the following to valid to dd-MMM-yyyy.

The calendar extender passes this format to the text box but is never shown in the text box.

The validation message appears for a fraction of a second and then all you see is the mark again
__-___-____

Example code:

<ajaxToolkit:CalendarExtender runat="server" Format="dd-MMM-yyyy" 
    TargetControlID="DOB" />

<ajaxToolkit:MaskedEditValidator ID="DOBMaskedEditValidator" 
    EmptyValueBlurredText="DOB required" EmptyValueMessage="DOB required" 
    InvalidValueBlurredMessage="DOB invalid" InvalidValueMessage="DOB invalid" 
    runat="server" ControlExtender="DobMaskedEditExtender" 
    SetFocusOnError="true" Display="Dynamic" ControlToValidate="DOB"
    IsValidEmpty="false" />

<ajaxToolkit:MaskedEditExtender ID="DobMaskedEditExtender" runat="server" 
    MaskType="Date" TargetControlID="DOB" Mask="99-LLL-9999" CultureName="en-GB" 
    ClearMaskOnLostFocus="false" />

I've been looking at various examples but can't seem to pin down what the issue is. Anyone got any ideas how we do this with a custom date format? Should I be using MaskType=Date with a custom date format?

1

There are 1 best solutions below

0
On BEST ANSWER

Sorry guys was overlooking the obvious. I had missed out the ValidationExpression attribute. I think because I had specified the format in the MaskedEditExtender I though that would be used alongside the validator.

A lot of examples out where show a regular expression like below:

"^(0?[1-9]|[12][0-9]|3[01])-(jan|Jan|JAN|feb|Feb|FEB|mar|Mar|MAR|apr|Apr|APR|may|
May|MAY|jun|Jun|JUN|jul|Jul|JUL|aug|Aug|AUG|sep|Sep|SEP|oct|Oct|OCT|nov|Nov|NOV|
dec|Dec|DEC)-(19|20)\d\d\s([0-1][0-9]|[2][0-3]):([0-5][0-9])$"

But I need this to work in multiple languages so have gone for a more basic one:

"(^\d{2}\-?\w{3}\-?\d{4}$)"

This is now working fine.

Fully working example:

<ajaxToolkit:CalendarExtender runat="server" Format="dd-MMM-yyyy"
TargetControlID="DOB"></ajaxToolkit:CalendarExtender>
<ajaxToolkit:MaskedEditValidator ID="DOBMaskedEditValidator" 
ValidationExpression="(^\d{2}\-?\w{3}\-?\d{4}$)" 
EmptyValueBlurredText="<%$Resources:Resource,DOBRequired%>" 
EmptyValueMessage="<%$Resources:Resource,DOBRequired%>" 
InvalidValueBlurredMessage="<%$Resources:Resource,DOBInvalid%>" 
InvalidValueMessage="<%$Resources:Resource,DOBInvalid%>" 
runat="server" ControlExtender="DobMaskedEditExtender" 
SetFocusOnError="true" Display="Dynamic" ControlToValidate="DOB"  
IsValidEmpty="false"></ajaxToolkit:MaskedEditValidator>
<ajaxToolkit:MaskedEditExtender ID="DobMaskedEditExtender" 
ClearTextOnInvalid="false" runat="server" TargetControlID="DOB" 
Mask="99-LLL-9999" CultureName="en-GB" ClearMaskOnLostFocus="false" >
</ajaxToolkit:MaskedEditExtender>