使用在FireFox中无效的附加javascript强制格式化日期文本框

时间:2022-12-06 18:01:59

The text box that I'm trying to force the formatting works in IE 8-11 and Chrome but not Firefox. The issue is that in Firefox you cant change the value in the text box at all. It's just stuck only way to change the value is the calendar next to the box. Here are the methods I'm using with related fields:

我试图强制格式化的文本框在IE 8-11和Chrome中运行,但不在Firefox中。问题是在Firefox中你根本不能改变文本框中的值。只是改变价值的方法只是盒子旁边的日历。以下是我在相关领域使用的方法:

     public static void RegisterMask(System.Web.UI.Page Caller)
     {
        System.Text.StringBuilder strMask = new System.Text.StringBuilder();

        strMask.Append("<script language=\"javascript\">");
        strMask.Append("function maskKeyPress(objEvent) {");
        strMask.Append(" var iKeyCode = objEvent.keyCode;  ");
        strMask.Append("if(iKeyCode>=48 && iKeyCode<=57){return true;}");
        strMask.Append("else{");
        strMask.Append("return false;}}");
        strMask.Append("</script>");
        strMask.Append("<script language=\"javascript\">");
        strMask.Append(" function mask(str,textbox,loc,delim,event){");
        strMask.Append("if (maskKeyPress(event)== false){return false;}");
        strMask.Append("var locs = loc.split(',');");
        strMask.Append("for (var i = 0; i <= locs.length; i++){");
        strMask.Append("for (var k = 0; k <= str.length; k++){");
        strMask.Append(" if (k == locs[i]){");
        strMask.Append(" if (str.substring(k, k+1) != delim){");
        strMask.Append("   str = str.substring(0,k) + delim + str.substring(k,str.length)}}}}textbox.value=str}");
        strMask.Append("</script>");
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Caller, Caller.GetType(), "Mask", strMask.ToString(), false);
     }
 protected override void Page_Load(object sender, EventArgs e)
 {
    RegisterMask(this.Page);

    if (!Page.IsPostBack) 
    { 
        pMask(this.Page, ref txtStart, "/", "2,5");
        pMask(this.Page, ref txtEnd, "/", "2,5");
    }
 }
            <cs:Row runat="server" ID="rwStartDate">
                <LabelTemplate><asp:Label runat="server" ID="lbltxtStart"      AssociatedControlID="txtStart">Start</asp:Label></LabelTemplate>
                <ControlTemplate>
                    <asp:TextBox runat="server" ID="txtStart" Width="100" EnableViewState="true" AutoPostBack="true"/>
                    <asp:RequiredFieldValidator runat="server" ID="rfvStartDate" ControlToValidate="txtStart" ErrorMessage="Invalid Date" SetFocusOnError="true" CssClass="validator">*</asp:RequiredFieldValidator>
                    <asp:CustomValidator runat="server" ID="csvStart" ControlToValidate="txtStart" ErrorMessage="Invalid Date" SetFocusOnError="true" CssClass="validator" ClientValidationFunction="Validator.validateDate" />
                    <asp:HyperLink NavigateUrl="#" runat="server" ID="lnkCalStart" ToolTip="Toggle Calendar" CssClass="calendar" />
                    <ajax:CalendarExtender runat="server" ID="ajxStart" OnClientDateSelectionChanged="calendarExtenderHelper" Animated="false" PopupPosition="Right" Format="M/d/yyyy" TargetControlID="txtStart" PopupButtonID="lnkCalStart" BehaviorID="ajxCalStart" />
                </ControlTemplate>
            </cs:Row>

1 个解决方案

#1


0  

Turns out it was the onkeypress not being supported by firefox and needed to be change to onkeyup. Also had an issue with numpad which needed another set of keycodes.

事实证明,firefox没有支持onkeypress,需要更改为onkeyup。还有numpad的问题需要另一组密钥代码。

#1


0  

Turns out it was the onkeypress not being supported by firefox and needed to be change to onkeyup. Also had an issue with numpad which needed another set of keycodes.

事实证明,firefox没有支持onkeypress,需要更改为onkeyup。还有numpad的问题需要另一组密钥代码。