【问题标题】:asp.net textbox with java script problem带有javascript问题的asp.net文本框
【发布时间】:2010-03-09 06:16:24
【问题描述】:

我正在尝试检查 asp.net 文本框是否为空或未使用 java 脚本。 我的代码工作正常,但如果 我只输入数字会认为它是空的,所以我要么输入字母和数字,要么只输入字母。

请指教。

这是我的代码

 <script type="text/javascript">
function check(){
  var txt = document.getElementById('<%=txt.ClientID%>').value;

              //(isNaN(cmbStateHome) == false

              if (isNaN(txt) == false) {
                  alert("Please enter some thing.");

              }
              else {alert("ok");}
          }
}
</script> 


 <asp:TextBox ID="txt" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtZipCodeHome" runat="server" Style="top: 361px; left: 88px; position: absolute;

<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="check()">LinkButton</asp:LinkButton>

【问题讨论】:

    标签: asp.net javascript


    【解决方案1】:

    替换

              if (isNaN(txt) == false) {
    

              if (txt == "") {
    

    for isNan 看here:

    isNaN 函数计算参数以确定它是否为“NaN”(不是数字)。

    【讨论】:

      【解决方案2】:

      isNAN 正在检查输入是否为数字。即:不是数字。

      因此,如果只输入数字,它将返回 false。

      您应该替换该检查以查看文本是否等于空字符串。

      【讨论】:

        【解决方案3】:

        你为什么不用if (txt.length == 0)

        【讨论】:

          【解决方案4】:

          看看使用&lt;asp:RequiredFieldValidator /&gt; 服务器控件。如果用户没有填写文本框,它将自动将正则表达式应用于该字段并阻止用户提交表单。

          例如)

          <asp:TextBox id="TextBox1" runat="server"/>
          <asp:RequiredFieldValidator id="req1" ControlToValidate="TextBox1" runat="server" />
          <asp:LinkButton id="Button1" Text="Do Something" runat="server" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-26
            相关资源
            最近更新 更多