代码之家  ›  专栏  ›  技术社区  ›  Eton B.

javascript window.event将为空?

  •  0
  • Eton B.  · 技术社区  · 14 年前

    我在ASP.NET中使用一个CustomValidator,如下所示:

    <asp:CustomValidator ID="cvComment" ControlToValidate="txtComment" Display="None"
            EnableClientScript="true" ClientValidationFunction="validateComment"
        runat="server" ></asp:CustomValidator>
    

    这个函数被调用:

    function validateComment(source, args) {
                var reComment = new RegExp("^[a-zA-Z0-9',!;?@#%*.\s]{1,1000}$");
                var validComment = reComment.test(window.event.srcElement.value);
                if (!validComment)
                    alert("The comment has illegal characters");
                args.IsValid = validComment;
            }
    

    单击触发验证器的按钮时,应用程序将中断,我可以看到 window.event 属性为空,因此显然有一个空引用试图匹配regEx。我的问题是为什么window.event会显示为空?我本可以发誓这之前是有效的。

    编辑:

    我对函数进行了如下修改:

       var check = document.getElementById(source.id);
       var checky = check.attributes["controltovalidate"].value;
       var checkyo = document.getElementById(checky);
       var validHour = reOutHour.test(checkyo.value);
       if (!validHour)
            alert("The time is incorrectly formatted");
       args.IsValid = validHour;
    

    现在这是在Internet Explorer上工作,但不是在Firefox上。。。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Eton B.    14 年前

    这就是我解决问题的方法:

    var check = document.getElementById(source.id);
       var checky = check.controltovalidate;
       var checkyo = document.getElementById(checky);
       var validHour = reOutHour.test(checkyo.value);
       if (!validHour)
            alert("The time is incorrectly formatted");
       args.IsValid = validHour;