javascript window.event coming up null?
I am using a CustomValidator in ASP.NET as follows:
<asp:CustomValidator ID="cvComment" ControlToValidate="txtComment" Display="None"
EnableClientScript="true" ClientValidationFunction="validateComment"
runat="server" ></asp:CustomValidator>
And开发者_如何学编程 this is the function that gets called:
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;
}
Upon clicking the button that triggers the validator, the application breaks and I can see that the window.event
property is null, so obviously there's a null reference trying to match the regEx. My question is why could the window.event be showing up as null? I could've sworn this was working before.
EDIT:
I have modified the function as such:
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;
Now this is working on Internet Explorer, but not on Firefox...
This is how I managed to solve my problem:
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;
精彩评论