我在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上。。。