我的aspx中有5个文本框。当我点击保存按钮时,如何验证用户至少填写任何3个文本框?有可能吗?感谢
ASPX:
<table width="100%">
<tr>
<td>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt3" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt4" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txt5" runat="server"></asp:TextBox>
</td>
</tr>
</table>
vb.net:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
End Sub
答案 0 :(得分:2)
<script type="text/javascript">
function Required() {
debugger;
var i = 0;
var get = $("input[type=text]");
get.each(function () {
if (this.value != "") {
i = i + 1;
}
})
if (i < 3) {
EnableValidator("RequiredFieldValidator1");
}
else {
DisableValidator("RequiredFieldValidator1");
}
}
function EnableValidator(id) {
if ($('#' + id)[0] != undefined) {
ValidatorEnable($('#' + id)[0], true);
$('#' + id).hide();
}
}
//Code:: Validator Disabled ::
function DisableValidator(id) {
if ($('#' + id)[0] != undefined) {
ValidatorEnable($('#' + id)[0], false);
}
}
</script>
只需放一个验证器就可以了。它会起作用。
答案 1 :(得分:1)
public Int32 FindNoOfTextBox()
{
Int32 count=0;
if (txt1.Text != "")
{
count++;
}
if (txt2.Text != "")
{
count++;
}
if (txt3.Text != "")
{
count++;
}
if (txt4.Text != "")
{
count++;
}
if (txt5.Text != "")
{
count++;
}
return count;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Int32 count=FindNoOfTextBox();
if (count >= 3)
{
//when 3 or more than 3 textboxs contains values
}
else
{
//less than three textboxes contains values
}
}
答案 2 :(得分:0)
您可以遍历文本框并计算包含数据的数字。
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim numFilled As Integer = 0
For index As Integer = 1 To 5
If Not String.IsNullOrEmpty(CType(FindControl("txt" & index), TextBox).Text) Then
numFilled += 1
End If
Next
If numFilled >= 3 Then
Else
End If
End Sub