【问题标题】:How can I change validation with alert message to validation with red borders around textboxes?如何将带有警报消息的验证更改为在文本框周围带有红色边框的验证?
【发布时间】:2017-08-18 18:19:47
【问题描述】:

我可以使用以下脚本在 gridview 控件中验证动态生成的文本框:

<script type="text/javascript">
function ValidateEmptyValue() { 
    var gv = document.getElementById("<%= Gridview1.ClientID %>"); 
    var tb = gv.getElementsByTagName("input"); 

    for (var i = 0; i < tb.length; i++) { 
        if (tb[i].type == "text") { 
            if (tb[i].value < 1) { 
                 alert("Field cannot be blank!");
                return false; 
            } 
        } 
    } 
    return true; 
}
</script>

这很好用。

但是,我想改变

alert("Field cannot be blank!");

文本框周围有红色边框?

提前致谢

        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
            <label ID="lblTitle">Your title<span style="color:#ff0000">*</span></label><br />
            <asp:RequiredFieldValidator id="RequiredFieldValidator2" Font-Bold="true" 
                BorderColor="Red" BorderWidth="1" SetFocusOnError="true" 
            runat="server" 
            Height="16px" ErrorMessage="REQUIRED FIELD" ControlToValidate="txtsourcename" />
                <asp:TextBox ID="txtsourcename" runat="server" style="width:200px;"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Adress">
            <ItemTemplate>
                <asp:TextBox ID="txtsourceaddress" runat="server" style="width:200px;"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Income">
            <ItemTemplate>
                 <asp:TextBox ID="txtsourceincome" runat="server" style="width:200px;"></asp:TextBox>
            </ItemTemplate>

    '//generated HTML code
<table cellspacing="0" rules="all" border="1" id="Gridview1" style="border-collapse:collapse;">
    <tr>
        <th scope="col">Name</th><th scope="col">Adress</th><th scope="col">Income</th><th scope="col">&nbsp;</th>
    </tr><tr>
        <td>
            <label ID="lblTitle">Your title<span style="color:#ff0000">*</span></label><br />
            <span id="Gridview1_RequiredFieldValidator2_0" style="display:inline-block;border-color:Red;border-width:1px;border-style:solid;font-weight:bold;height:16px;visibility:hidden;">REQUIRED FIELD</span>
                <input name="Gridview1$ctl02$txtsourcename" type="text" id="Gridview1_txtsourcename_0" style="width:200px;" />
            </td><td>
                <input name="Gridview1$ctl02$txtsourceaddress" type="text" id="Gridview1_txtsourceaddress_0" style="width:200px;" />
            </td><td>
                 <input name="Gridview1$ctl02$txtsourceincome" type="text" id="Gridview1_txtsourceincome_0" style="width:200px;" />
            </td><td><a href="javascript:__doPostBack(&#39;Gridview1&#39;,&#39;Delete$0&#39;)">Delete</a></td>
    </tr><tr>
        <td>&nbsp;</td><td>&nbsp;</td><td align="right">
             <input type="submit" name="Gridview1$ctl03$ButtonAdd" value="Add More" onclick="return ValidateEmptyValue();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Gridview1$ctl03$ButtonAdd&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="Gridview1_ButtonAdd" />
            </td><td>&nbsp;</td>
    </tr>
</table>

【问题讨论】:

  • 而不是alert() 使用tb[i].style.border = "solid 1px red";
  • @AZee,非常感谢。它适用于一个文本框。我在 gridview1 上有三个文本框控件。是否有可能让它同时在所有三个上设置边界?这是不可能的,那么我坚持这个解决方案。
  • 我刚刚更新了代码! @肯尼

标签: jquery css asp.net


【解决方案1】:

当您需要更改输入字段的样式属性时,请使用style.border

以下是您的代码的更新版本。

<script type="text/javascript">
function ValidateEmptyValue() { 
    var is_valid = true,
        gv = document.getElementById("<%= Gridview1.ClientID %>"),
        tb = gv.getElementsByTagName("input");

    for (var i = 0; i < tb.length; i++) { 
        if (tb[i].type == "text") { 
            if (tb[i].value < 1) { 
                tb[i].style.border = "solid 1px red";
                is_valid = false;
            } 
        }
    }

    return is_valid;
}
</script>

【讨论】:

  • 谢谢,但它仍然突出显示一个文本框而不是三个。我已经显示了上面的三个文本框。
  • 对不起,我不熟悉 asp,但如果你能告诉我生成的 HTML 会有所帮助。 @肯尼
  • 我已经更新了上面的代码以显示生成的 HTML。我认为您使用所有三个文本框的原因是因为它有一个名称。
  • 我认为问题在于您的每个文本框都位于不同的 GridView 中。您要么需要var gv1 = document.getElementById("&lt;%= Gridview1.ClientID %&gt;"); var gv2 = document.getElementById("&lt;%= Gridview2.ClientID %&gt;"); var gv3 = document.getElementById("&lt;%= Gridview3.ClientID %&gt;");,要么将所有三个网格视图包装到一个容器中并为其分配一个唯一的ID
  • 你在哪里看到的?我更新了代码,因为我在那里添加了比我想要的更多的代码,
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多