ASP.NET 2.0自定义客户端验证在Internet Explorer中不起作用

ASP.NET 2.0 Custom Client Side Validation not working in Internet Explorer

本文关键字:Internet Explorer 不起作用 验证 客户端 NET 自定义 ASP      更新时间:2023-09-26

我有两个文本框,一个用于指定日期,另一个用于确定时间。如果两个文本框都为空,或者都有内容,我希望通过验证。如果只有一个内容,我希望验证失败。服务器端运行良好,以下客户端代码在Chrome中运行正常。

    function CheckScheduledDateTime(sender, args) {
        if (ctl00_MainContent_txtScheduledTime.value!="" || ctl00_MainContent_txtScheduledDate.value!="" )
        {
            if (ctl00_MainContent_txtScheduledTime.value!="" && ctl00_MainContent_txtScheduledDate.value=="")
            {
                args.IsValid=false;
                alert("Scheduled date is required");
            }
            else if (ctl00_MainContent_txtScheduledTime.value=="" && ctl00_MainContent_txtScheduledDate.value1!="")
            {
                args.IsValid=false;
                alert("Scheduled time is required");
            }
            else
            {
                args.IsValid=true;
            }
        }
        else 
        {
            args.IsValid = true;
        }         
    }

在Internet Explorer中,它不起作用,我得到以下错误:

"Microsoft JScript运行时错误:'ctl00_MainContent_txtScheduledTime'未定义"

奇怪的是,当它在Visual Studio中崩溃时,如果我再次尝试进入,它就会再次崩溃,但如果我第三次尝试进入,验证就会正常工作。

有人能对此有所了解吗?

您可以像这样使用

ctl00_MainContent_txtScheduledTime不是javascript变量,除非您使用初始化它们

var ctl00_MainContent_txtScheduledTime = document.getElementById('<%=txtScheduledTime.ClientID%>');

或者你可以像一样使用它

(document.getElementById('<%=txtScheduledTime.ClientID%>').value!="" || document.getElementById('<%=txtScheduledDate.ClientID%>').value!="" )

谨致问候。