1
我在窗體上有多個文本字段,我希望他們兩個應該驗證,如果其中一個是空的,然後說「兩個字段都是必需的」。此外,我還在表單上顯示其他文本字段,並且它們已經通過單擊按鈕進行驗證。驗證使用javascript的相互輸入輸入
它可以使用Asp.Net CustomValidator處理嗎?
我在窗體上有多個文本字段,我希望他們兩個應該驗證,如果其中一個是空的,然後說「兩個字段都是必需的」。此外,我還在表單上顯示其他文本字段,並且它們已經通過單擊按鈕進行驗證。驗證使用javascript的相互輸入輸入
它可以使用Asp.Net CustomValidator處理嗎?
您可以使用自定義驗證做這個任務。
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="testValid"
ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate"
ValidateEmptyText="True">both fields required</asp:CustomValidator>
ClientValidationFunction
的包含在客戶端javascript函數testValid
。所以它應該是這樣的:
<script type="text/javascript">
function testValid(sender, args) {
....you logic
//set args.IsValid according to your logic
args.IsValid = false;
}
</script>
在服務器端,
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//set args.IsValid according to your validation logic.
args.IsValid = false;
}
使用比較和需要現場驗證,即
<label>Password</label> <asp:TextBox runat="server" ID="txtPassword" MaxLength="15" TextMode="Password" />
<label>Password-check</label> <asp:TextBox runat="server" ID="txtPasswordCheck" TextMode="Password" MaxLength="15" />
<asp:RequiredFieldValidator runat="server" ID="rfvtxtPasswordCheck" ControlToValidate="txtPasswordCheck" Text="* " />
<asp:CompareValidator runat="server" ID="cvtxtPasswordCheck" ControlToValidate="txtPasswordCheck" ControlToCompare="txtPassword" Operator="Equal" Type="String" Text="* Passwords do not match" />