4
Vote

MaskedEdit + DateTime bug

description

I use a masked edit control to user input date and time, but I have a problem. When user input date and time, control replace time on date.

Example:

User input:

01.01.2011 12:20:33

Unfocus. Control replace time on date:

01.01.2011 01.01.20

Page:
            <asp:TextBox ID="FromDate" runat="server"></asp:TextBox>
            <asp:MaskedEditExtender ID="FromDateTimeMaskedEdit" runat="server" 
                            TargetControlID="FromDate"                                
                            Mask="99/99/9999 99:99:99"
                            MessageValidatorTip="true"
                            OnFocusCssClass="timeeditfocus"
                            OnInvalidCssClass=".timeeditinvalid"
                            MaskType="DateTime"
                            ErrorTooltipEnabled="True"
                            UserDateFormat="DayMonthYear"
                            UserTimeFormat="TwentyFourHour" />
            <asp:MaskedEditValidator ID="FromDateTimeMaskedEditValidator" runat="server" 
                            ControlExtender="FromDateTimeMaskedEdit"
                            ControlToValidate="FromDate"
                            IsValidEmpty="False"
                            EmptyValueMessage="Date and time are required"
                            InvalidValueMessage="Date and/or time is invalid"
                            Display="Dynamic"
                            TooltipMessage="Input a date and time"
                            EmptyValueBlurredText="*"
                            InvalidValueBlurredMessage="*" />
PS: Sorry for my bad english.

comments

bocca wrote Oct 17, 2011 at 1:41 PM

I have found a solution for version 4.1.50401.0.

If you can/want to modify source code of AjaxControlToolkit and recompile it, go to MaskedEdit.MaskedEditBehavior.js, search de definition of function AutoFormatTime, and replace the following:
"A[0]" to "A[A.length-1]"
"A[1]" to "A[A.length-1]"
"x[0]" to "x[x.length-1]"
"x[1]" to "x[x.length-1]"
If you cannot/don't want to modify source code, add the following javascritpt snipper somewhere after the inclusion of the combined scripts of AjaxControlToolkit:
Sys.Extended.UI.MaskedEditBehavior.prototype.AutoFormatTime = new Function(
    'var g=this,c=-1,e=" ",a="",b=false,d=true,h="0";' +
    Sys.Extended.UI.MaskedEditBehavior.prototype.AutoFormatTime.toString().substring(10)
    .replace("A[0]", "A[A.length-1]").replace("A[1]", "A[A.length-1]")
    .replace("x[0]", "x[x.length-1]").replace("x[1]", "x[x.length-1]"));

bocca wrote Oct 18, 2011 at 8:44 AM

Better code for the above (tested with 4.1.50401.0 binaries):

try { p = Sys.Extended.UI.MaskedEditBehavior.prototype; } catch (e) { p = null; }
if (p != null) {
p.AutoFormatTime = new Function(
    'var g=this,c=-1,e=" ",a="",b=false,d=true,h="0";' +
    funcbody(p.AutoFormatTime)
    .replace("A[0]", "A[A.length-1]").replace("A[1]", "A[A.length-1]")
    .replace("x[0]", "x[x.length-1]").replace("x[1]", "x[x.length-1]"));
}

function funcbody(f) {
var s = f.toString();
return s.substring(s.indexOf('{'));
}