验证特定条件的控制 [英] Validate Control on Specific condition
本文介绍了验证特定条件的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我想在特定情况下执行必要的字段验证..
示例:假设我有1个下拉列表,2个文本框和1个提交按钮
如果下拉选择值为1,那么1textbox应该在提交按钮点击时执行必要的字段验证或
否则2textbox执行验证。
Hello,
I want to perform required field validation on specific condition..
example: suppose I have 1 dropdown, 2 textbox and 1 submit button
if dropdown selectvalue is "1" then 1textbox should be perform required field validation on submit button click or
otherwise 2textbox perform validation.
推荐答案
试试这个样本。
try this sample.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-1.10.2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
</asp:DropDownList>
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
</body>
</html>
代码落后:
code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblMessage.Text = "";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (ddl.Text == "1")
{
if (TextBox1.Text == "")
{
lblMessage.Text = "please enter TextBox1 text";
return;
}
}
else if (ddl.Text == "2")
{
if (TextBox2.Text == "")
{
lblMessage.Text = "please enter TextBox2 text";
return;
}
}
lblMessage.Text = "success";
}
我理解的内容如下
你有两个文本框到在自定义条件下进行验证。
如果DropDown.SelectedIndex = 1则仅验证TextBox1
OTHERWISE
验证两个文本框
解决方案1 :
DropDown的SelectedIndexChange =>
检查IF [SelectedIndex = 1 ]然后禁用TextBox2验证器ELSE启用TextBox2验证器
Solution2 :
使用此链接中使用的自定义验证程序 [ ^ ]
What i understood is as follows
You have two textboxes to be validated on custom provided conditions.
If DropDown.SelectedIndex = 1 Then Validate only TextBox1
OTHERWISE
Validate both TextBoxes
Solution1:
On SelectedIndexChange of DropDown =>
Check IF[SelectedIndex = 1] THEN disable TextBox2 validator ELSE Enable TextBox2 validator
Solution2:
Use Custom validator as used in this link[^]
试试这个javascript函数: -
try this javascript function:-
function validate() {
if (document.getElementById("<%= ddlSlct.ClientID%> ").value == "1") {
if (document.getElementById("<%= taxbox1.ClientID%> ").value != "") {
alert("textbox1 is empty");
}
else {
if (document.getElementById("<%= taxbox2.ClientID%> ").value != "") {
alert("textbox2 is empty");
}
}
}
<asp:Button ID="btnSubmit" runat="server" OnClientClick="validate();" />
根据您的验证更改功能。
i只是检查它是否是是否空。
make changes in function as per your validation.
i have just check if it is empty or not.
这篇关于验证特定条件的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文