[英]Check the total of empty textbox in aspx
我的aspx中有5个文本框。 当我单击“保存”按钮时,如何验证用户至少填写任何三个文本框? 有可能这样做吗? 谢谢
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
<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>
只需放置一个验证器并执行此操作即可。 它会工作。
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
}
}
您可以遍历文本框并计算包含数据的数量。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.