4

Closed

MaskedEditExtender does not handle century properly in a date mask

description

When using a MaskedEditExtender with the mask attribute set to "99/99/9999" and the MaskType attribute set to "Date", the extender does not handle the century properly. No matter what the AutoComplete attribute is set to, if a user enters a two digit year (for example: "08") the masked edit will complete the year as 1908. It should at least use the current century, which can be accomplished like so:
var right_now=new Date();
[some variable] = right_now.getYear();

In my pages, I have used a hack - it works, but it's stupid to have to do this, and it causes a post back, which makes the page lose focus (which can be annoying when you're trying to tab through the controls on the list, and it goes to the default button after you go through a date text box). The following is a method I attach to the "OnTextChanged" attribute of a textbox:
    protected void DateBox_TextChanged(object sender, System.EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        Boolean blnReturnDate = false;
        string strDateText = tb.Text;
        char[] splitter  = {'/'};
        string[] strArrDate = new string[3];
        string strYear = "";

        try
        {
            strArrDate = strDateText.Split(splitter);
        }
        catch
        {
            //do nothing
        }

        if (strArrDate.Length < 3)
        {
            strArrDate = new string[3];
            strArrDate[2] = "";
        }

        string strLastCentury;
        string strCurrCentury;

        strCurrCentury = Convert.ToString(System.DateTime.Now.Year).Substring(0, 2) + "00";
        strLastCentury = Convert.ToString(Convert.ToInt32(strCurrCentury) - 100);

        // Check the length of the year, if 2 then re-calculate
        if (strArrDate[2].Replace("_", "").Length == 2)
        {
            blnReturnDate = true;
            strYear = strArrDate[2].Replace("_", "");

            if (Convert.ToInt32(strYear) >= 49)
            {
                strYear = strLastCentury.Substring(0, 2) + strYear;
            }
            else
            {
                strYear = strCurrCentury.Substring(0, 2) + strYear;
            }
        }
        else if (strArrDate[2].Replace("_", "").Length < 2)
        {
            strYear = Convert.ToString(System.DateTime.Now.Year);
        }

        if (blnReturnDate)
        {
            tb.Text = strArrDate[0] + "/" + strArrDate[1] + "/" + strYear;
        }
    }

file attachments

Closed May 3, 2013 at 7:09 AM by Superexpert
Issues is closed as it is fixed with April 2013 release.

comments

dtguitarfan wrote Apr 21, 2008 at 2:55 PM

I believe I have figured out how to fix this issue. I have made code changes to the MaskedEditBehavior.js file in the sections that start with the following line:
if (Y4)

Note that this line occurs twice, and in both instances, I had to create a few new variables before the line. The logic I used is as follows (written in simple english):
if the text provided for the year is not 4 characters in length, but is at least 1 character in length, first take the current year (not hard-coded, but coded to get the current year so it will work in 100 years even), and then extract the length of what is provided from the current year (for example, if the current year is 2008, and "05" is provided, what you will end up with is "20"). Add what is left to the beginning of what has been provided in the text box (instead of "0", which is what was there before, and makes no sense). Then we do a test: if the integer value of what we have now is more than this year + 50, then we subtract 100 from what we have now, or if the integer of this year is more than what we have now + 50, then we add 100. So for example, if "96" is what was provided, and this year is 2008, it will use 1996 instead of 2096, and if the current year is 2060 and "08" is provided, 2108 will be used. Thus, when autofilling, the year will always be within 50 if the current year.

singlecarrier wrote May 17, 2008 at 4:15 AM

dtguitarfan
It appears that there is a bug in this new code. If I set the Mask to 99/99/99 , the following code will fail because the Ycur instance in the else statement is not set to a value when the substring is called:
        if (Y4)
        {
            Ycur = CurDate.getUTCFullYear().toString();
        }
        else
        {
            Ycur = Ycur.substring(2); // this line fails because Ycur does not have a value assigned to it
        }
perhaps the following code will fix the issue:
        if (Y4)
        {
            Ycur = CurDate.getUTCFullYear().toString();
        }
        else
        {
            // set Ycur before attempting to use it
            Ycur = CurDate.getUTCFullYear().toString();
            Ycur = Ycur.substring(2); // this line fails because Ycur does not have a value assigned to it
        }

walterwillis wrote Jul 30, 2008 at 9:05 PM

Why not use the property of Century? This seems to be what it was intended for according to the description - it is a user configurable value to be used to complete the year when the user types in only two digits of the year.

My solution is similar - except I am using Century and doing a substring(0, 4 - YCur.length) to get the remaining digits. Since I am taking Century I am not checking any boundry. But I may incorporate that into my fix.

Walter

walterwillis wrote Jul 30, 2008 at 9:30 PM

Another quick question on fixing this problem. There is a function called ConvFmtDate that appears to be used to convert the date string to the correct display format. There is a while loop there for the year that pads with zero:
    .
    var Y = parseInt(m_arrDate[this.get_CultureDateFormat().indexOf("Y")],10) + '';
    while (Y.length < m_mask[this.get_CultureDateFormat().indexOf("Y")].length)
    {
        Y = "0" + Y;
    }
    m_arrDate[this.get_CultureDateFormat().indexOf("Y")] = Y;
Did you consider making the same update here?

Walter

wrote Sep 5, 2008 at 11:51 PM

Not sure if this isn't just a JavaScript date parsing issue, which might introduce a difference between the same field parsed as a data through plain script and what this behavior would result in.

dcumbow wrote Feb 23, 2010 at 12:08 AM

Has there been any movement on fixing the Century issue with the MaskedEditExtender?

Thanks,
David