gpt4 book ai didi

javascript - ASP.NET 自定义验证器客户端和服务器端验证

转载 作者:行者123 更新时间:2023-11-28 05:13:02 26 4
gpt4 key购买 nike

在我的网页中,我尝试在客户端和服务器端执行验证。如果下拉菜单和文本框为空,我需要显示一条错误消息,但如果其中之一已填写,则验证应该通过。有没有办法为这两个控件创建一个 CustomValidator?我感觉我做得不对。

客户端代码:

 <div>
<table>
<tr>
<td>
<asp:DropDownList ID="ddlStates" runat="server" Width="128px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Nevada</asp:ListItem>
<asp:ListItem>Uta</asp:ListItem>
<asp:ListItem>Oregon</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:CustomValidator ID="cvddlState" runat="server"
ClientValidationFunction="StatesCheck"
OnServerValidate="ServerValidation"
ErrorMessage="(*) State is required" ForeColor="Red"
Display="Dynamic"></asp:CustomValidator>
</td>

</tr>
<tr>
<td>
<asp:TextBox ID="txtStates" runat="server"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="cvtxtStates" runat="server"
ValidateEmptyText="true"
ClientValidationFunction="StatesCheck"
OnServerValidate="ServerValidation"
ControlToValidate="txtStates"
ErrorMessage="(*) Text cannot be empty" ForeColor="Red"
Display="Dynamic"></asp:CustomValidator>
</td>

</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>

<div id="divErrorMessage" runat="server" class="alert alert-danger" visible="false"></div>


<script type="text/javascript">

'Use Strict';
function StatesCheck(source, args) {
var ddlStates = document.getElementById("<%=ddlStates.ClientID%>");
var txt = document.getElementById('<%=txtStates.ClientID%>').value;
var state = ddlStates.options[ddlStates.selectedIndex].value;

if (ddlStates !== null) {
if ((state === "") && (txt === "")) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}

</script>

服务器端代码:

Public Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Try
divErrorMessage.Visible = False
divErrorMessage.InnerText = ""


Dim ddlSelection As String = ddlStates.SelectedItem.Text
Dim statesText As String = txtStates.Text.Trim()
If statesText = String.Empty And ddlSelection = String.Empty Then
Else
divErrorMessage.Visible = True
divErrorMessage.InnerText = "(*) Text cannot be empty"
End If

Catch ex As Exception

End Try
End Sub

Protected Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
If (ddlStates.SelectedItem.Text = String.Empty) And (txtStates.Text.Length = 0) Then
args.IsValid = False
Else
args.IsValid = True
End If

End Sub

最佳答案

您是否从服务器端和客户端的代码中得到答案?我想回答服务器端

Protected Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
If (ddlStates.SelectedItem.Text = String.Empty) And (txtStates.Text.Length = 0) Then
args.IsValid = False
Else
args.IsValid = True
End If

End Sub

根据我的经验,我不太擅长 ASP.net,但根据上面的 ypur 代码,没有任何问题,但是你所说的部分 但是如果填写其中之一,验证应该通过 此处未显示,您可以尝试执行此操作吗?

 Protected Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
If (ddlStates.SelectedItem.Text = String.Empty) Or (txtStates.Text.Length = 0) Then
args.IsValid = False
Else
args.IsValid = True
End If

End Sub

或者也许

Protected Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
If (ddlStates.SelectedItem.Text = String.Empty) And (txtStates.Text.Length = 0) Then
args.IsValid = False
ElseIf (ddlStates.SelectedItem.Text <> String.Empty) then
args.IsValid = True
ElseIf (txtStates.Text <> "") then
args.IsValid = True
Else

args.IsValid = True
End If

End Sub

关于javascript - ASP.NET 自定义验证器客户端和服务器端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41213605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com