【问题标题】:Require field validation is not working while adding other JavaScript添加其他 JavaScript 时要求字段验证不起作用
【发布时间】:2026-01-06 22:15:02
【问题描述】:

我的网页具有三个视图,在第二个视图上我想验证一个 JavaScript 函数。如果我不添加此函数,那么当我添加此 JavaScript 函数时,require 字段验证工作正常,并尝试在页面加载时调用 require 字段验证不起作用。

function checkDate()
    {           
        var dt = new Date();
        var month =1 + dt.getMonth();
        var day = dt.getDate();
        var e = document.getElementById("<%=PurchMonth.ClientID%>");
        var monthvalue = e.options[e.selectedIndex].value;
        var e1 = document.getElementById("<%=PurchDay.ClientID%>");
        var dayvalue = e1.options[e1.selectedIndex].value;

        if (monthvalue < month)
        {                  
           return true;
        }
        else if (monthvalue ==month )
        {
            if (dayvalue>day)
            {
                alert("Future Date is not allowed");
                return false;
            }
            else
            {
                return true;
            }
        }
        else                
        {
            alert("Future Date is not allowed");
            return false;
        }
    }

 protected void Page_Load(object sender, EventArgs e)
    {
        btnModelsNext.Attributes.Add("onclick", "return checkDate();");
    }

【问题讨论】:

    标签: javascript c# jquery asp.net validation


    【解决方案1】:

    我已更改以下代码

    protected void Page_Load(object sender, EventArgs e)
        {
            btnModelsNext.Attributes.Add("onclick", "return Validate();");
        }
    

    JavaScript 如下

    function Validate() {
            if (Page_ClientValidate()) {
                return checkDate();
            }
            return false;
        }
    
        function checkDate()
        {  rest of the code
            }
    

    【讨论】: