单击"搜索"按钮后未收到客户端消息

che*_*bli 5 javascript c# asp.net validation

我正在努力alpha.dubaiexporters.com.有一个搜索面板包含两个输入关键字和类别.我想验证关键字部分.

如果用户输入少于三个字符并单击"搜索"按钮,则应显示客户端消息the entered input should be more than 3 characters.我没有收到客户留言.

以下是我的代码:

<input type="text" id="txtkeyword" name="s" runat="server" autocomplete="off">
<asp:Button ID="Search" class="dt-header-search-submit dt-button dt-button-danger" style="top:0px;width:226px;height:70px;" Text="Search" runat="server" onclick="doit" OnClientClick="return checkLength();" />
Run Code Online (Sandbox Code Playgroud)

以下是我的Javascript:

<script type="text/javascript">
        function checkLength() {
            var textbox = document.getElementById("txtkeyword");
            if (textbox.value.length < 3) {
                alert("The entered input should be 3 or more than 3 characters");
                return false;
            }
        }
    </script>
Run Code Online (Sandbox Code Playgroud)

代码背后:

protected void doit(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtkeyword.Value))
        {
            try
            {

                String mainsearch1 = null;            
                mainsearch1 = txtkeyword.Value.Trim();
                if (mainsearch1.Length > 2)
                {
                    Response.Redirect("searchmain.aspx?mainsearch=" + mainsearch1 + "&querylevel=1");
                }


            }
            catch (Exception ex)
            {
            }
        }

        else if (!string.IsNullOrEmpty(txtserach.Value))
        {
            try
            {

                String cat = null;               
                cat = txtserach.Value.Trim();
                if (cat.Length > 2)
                {                 
                    Response.Redirect("searchcat1.aspx?cat=" + cat);
                }


            }
            catch (Exception ex)
            {
            }



        }


    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它不调用javascript函数.

预期输出:当用户在txtkeyword文本框中输入少于三个字母时,我想要此警报消息.

在此输入图像描述

小智 2

效果很好,请尝试一下:)

function checkLength() {
debugger;
var textbox =$get("<%=txtkeyword.ClientID%>"); //document.getElementById("<%=txtkeyword.ClientID%>")
if (textbox.value.length < 3) {
alert("The entered input should be 3 or more than 3 characters");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)

注意:当 runat="server" 存在时,您无法直接访问控件