验证控制的条件触发 [英] Conditional firing of validational control
问题描述
我的网络表单中有4个字段.
其中有我对其中的4个进行了验证控制.
< b>现在,我希望它们仅在其中一个(4个字段)被填充并且用户单击提交按钮时才触发.
但是,如果所有4个字段都保留为空白,则不应触发任何验证程序.</b>
为此,我编写了JavaScript函数validate().
我怀疑有一些小错误,这就是为什么它不起作用的原因.
请让我知道我的错误在哪里.
请检查||和&&句法.
我的页面如下所示:
< pre lang ="xml">& lt;%@ Page Language =& quot; C#& quot; AutoEventWireup =& quot; true& quot; CodeBehind =& quot; Default.aspx.cs& quot; Inherits =&"WebApplication1._Default& quot; %& gt;
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN&"& quot; http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd& quot;
< html xmlns =&"http://www.w3.org/1999/xhtml" & gt;
& lt; head runat =& quot;服务器& quot;"
& lt; title& gt;无标题页面& lt;/title& gt;
& lt; style type =& quot; text/css& quot;& gt;
.style1
{
宽度:27%;
高度:233像素;
}
.style2
{
宽度:68像素;
}
.style3
{
宽度:208px;
}
& lt//style& gt;
& lt//head& gt;
& lt; body& gt;
& lt; form id =& quot; form1& quot; runat =& quot;服务器& gt;
& lt; div& gt;
& lt; script language =& quot; javascript& quot; type =& quot;文本/javascript& quot;& gt;
函数validate()
{
var emailPat =/^(\&".*\&"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3 }){3}] || [A-Za-z] \ w *(\.[A-Za-z] \ w *)+)$/;
var emailid = document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot;).value;
var matchArray = emailid.match(emailPat);
var Url =&"^ [A-Za-z] +://[A-Za-z0-9-_] + \\.[A-Za-z0-9 -_%& \ ?\/.=] + $& quot;
var tempURL = document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot;).value;
var matchURL = tempURL.match(Url);
if(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value!=& quot;&))||(document.getElementById(& ;& lt;%= txtEmail.ClientID%& gt;").value!=&&")||(document.getElementById(&& lt; %= txtWebURL.ClientID%&>).value!=&"&)||(document.getElementById(&"%= txtZIP.ClientID%& ; gt;& quot).value!=& quot;& quot;)
{
如果(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value!=& quot;& quot;)
{
alert(& quot;名称Feild不能为空& quot;);
document.getElementById(&& lt;%= txtName.ClientID%& gt;&").focus();
返回false;
}
否则if(document.getElementById(& lt;%= txtEmail.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
警报(& quot;电子邮件ID不能为空& quot;);
document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot)).focus();
返回false;
}
否则,如果(matchArray == null)
{
警报(&您的电子邮件地址似乎不正确.请重试.&");
document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot)).focus();
返回false;
}
否则if(document.getElementById(& lt;%= txtWebURL.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
alert(&"Web URL不能为空&");
document.getElementById(& lt;%= txtWebURL.ClientID%& gt;& quot)).value =& quot; http://& quot;
document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot)).focus();
返回false;
}
否则if(matchURL == null)
{
alert(&"Web URL无效");
document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot)).focus();
返回false;
}
否则((document.getElementById(& lt;%= txtZIP.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
alert(& quot;邮递区号无效"));
document.getElementById(&& lt;%= txtZIP.ClientID%& gt;&").focus();
返回false;
}
返回true;
}
if(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value ==& quot;& quot;)& amp; amp; (document.getElementById(& lt;%= txtEmail.ClientID%& gt;& quot;).value ==& quot;& quot;)& amp; amp;( document.getElementById(& lt;%= txtWebURL.ClientID%& gt;&).value ==&"&)& amp; amp;(文档.getElementById(& lt;%= txtZIP.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
返回true;
}
}
& lt//script& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt; table class =& quot; style1& quot;"
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label4& quot; runat =&服务器"文字=& quot;名称& gt;& lt;/asp:标签& gt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtName& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label2& quot; runat =&服务器"文字=& quot;年龄>& lt;/asp:Label& gt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtEmail& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label3& quot; runat =&服务器"文字=& quot; Web URL& gt;& lt;/asp:Label& gt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtWebURL& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
& nbsp;</td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; TextBox1& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
& nbsp;</td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:Button ID =& quot; btnSubmit& quot; OnClientClick =& quot;返回validate()" runat =& quot;服务器& quot;
Text =&"Submit&" onclick =& quot; btnSubmit_Click& quot; /& gt;
& lt//td& gt;
& lt//tr& gt;
& lt//table& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt//div& gt;
& lt//form& gt;
& lt//body& gt;
& lt;/html& gt;</pre>
//btnSubmit_Click事件,它将在//验证页面后执行服务器端活动
< pre lang ="cs">受保护的无效btnSubmit_Click(对象发送者,EventArgs e)
{
//一些服务器端操作
TextBox1.Text =&"voila& quot ;;
}</pre>
Hi,
I have 4 fields in my web form.
Among them I have validation controls over 4 of them.
<b>Now,I want them to fire only when one of them(those 4 fields) is filled and user clicks submit button .
But,if all the 4 fields are kept blank,then there should not be any validator firing.</b>
To do that,I have written the javascript function validate().
I doubt there is some minor mistake,that''s why it is not working.
Please let me know where is my mistake.
Pls check the || and && syntax.
My page looks like below:
<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 27%;
height: 233px;
}
.style2
{
width: 68px;
}
.style3
{
width: 208px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<script language="javascript" type="text/javascript">
function validate()
{
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if (document.getElementById("<%=txtName.ClientID%>").value!="")||(document.getElementById("<%=txtEmail.ClientID %>").value!="")||(document.getElementById("<%=txtWebURL.ClientID %>").value!="")||(document.getElementById("<%=txtZIP.ClientID%>").value!="")
{
if (document.getElementById("<%=txtName.ClientID%>").value!="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
else if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
else if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
else if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
else if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
else if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
return true;
}
if (document.getElementById("<%=txtName.ClientID%>").value=="")&&(document.getElementById("<%=txtEmail.ClientID %>").value=="")&&(document.getElementById("<%=txtWebURL.ClientID %>").value=="")&&(document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
return true;
}
}
</script>
<br />
<br />
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="Name"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Web URL"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtWebURL" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server"
Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html></pre>
//btnSubmit_Click event which will do server side activity after //validating the page
<pre lang="cs">protected void btnSubmit_Click(object sender, EventArgs e)
{
//some server side action
TextBox1.Text = "voila";
}</pre>
推荐答案
/;
var emailid = document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot;).value;
var matchArray = emailid.match(emailPat);
var Url =&"^ [A-Za-z] +://[A-Za-z0-9-_] + \\.[A-Za-z0-9 -_%& \ ?\/.=] +
/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+
& quot;
var tempURL = document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot;).value;
var matchURL = tempURL.match(Url);
if(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value!=& quot;&))||(document.getElementById(& ;& lt;%= txtEmail.ClientID%& gt;").value!=&&")||(document.getElementById(&& lt; %= txtWebURL.ClientID%&>).value!=&"&)||(document.getElementById(&"%= txtZIP.ClientID%& ; gt;& quot).value!=& quot;& quot;)
{
如果(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value!=& quot;& quot;)
{
alert(& quot;名称Feild不能为空& quot;);
document.getElementById(&& lt;%= txtName.ClientID%& gt;&").focus();
返回false;
}
否则if(document.getElementById(& lt;%= txtEmail.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
警报(& quot;电子邮件ID不能为空& quot;);
document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot)).focus();
返回false;
}
否则,如果(matchArray == null)
{
警报(&您的电子邮件地址似乎不正确.请重试.&");
document.getElementById(&& lt;%= txtEmail.ClientID%& gt;& quot)).focus();
返回false;
}
否则if(document.getElementById(& lt;%= txtWebURL.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
alert(&"Web URL不能为空&");
document.getElementById(& lt;%= txtWebURL.ClientID%& gt;& quot)).value =& quot; http://& quot;
document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot)).focus();
返回false;
}
否则if(matchURL == null)
{
alert(&"Web URL无效");
document.getElementById(&& lt;%= txtWebURL.ClientID%& gt;& quot)).focus();
返回false;
}
否则((document.getElementById(& lt;%= txtZIP.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
alert(& quot;邮递区号无效"));
document.getElementById(&& lt;%= txtZIP.ClientID%& gt;&").focus();
返回false;
}
返回true;
}
if(document.getElementById(& lt;%= txtName.ClientID%& gt;& quot;).value ==& quot;& quot;)& amp; amp; (document.getElementById(& lt;%= txtEmail.ClientID%& gt;& quot;).value ==& quot;& quot;)& amp; amp;( document.getElementById(& lt;%= txtWebURL.ClientID%& gt;&).value ==&"&)& amp; amp;(文档.getElementById(& lt;%= txtZIP.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
返回true;
}
}
& lt//script& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt; table class =& quot; style1& quot;"
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label4& quot; runat =&服务器"文字=& quot;名称& gt;& lt;/asp:标签& gt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtName& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label2& quot; runat =&服务器"文字=& quot;年龄& gt;& lt;/asp:标签& lt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtEmail& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
< asp:Label ID =& quot; Label3& quot; runat =&服务器"文字=& quot; Web URL& gt;& lt;/asp:Label& gt;
& lt//td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; txtWebURL& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
& nbsp;</td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:TextBox ID =& quot; TextBox1& quot; runat =& quot;服务器& gt;& lt;/asp:TextBox& gt;
& lt//td& gt;
& lt//tr& gt;
& lt; tr& gt;
& lt; td class =& quot; style2& quot;& gt;
& nbsp;</td& gt;
& lt; td class =& quot; style3& quot;& gt;
& lt; asp:Button ID =& quot; btnSubmit& quot; OnClientClick =& quot;返回validate()" runat =& quot;服务器& quot;
Text =&"Submit&" onclick =& quot; btnSubmit_Click& quot; /& gt;
& lt//td& gt;
& lt//tr& gt;
& lt//table& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt//div& gt;
& lt//form& gt;
& lt//body& gt;
& lt;/html& gt;</pre>
//btnSubmit_Click事件,它将在//验证页面后执行服务器端活动
< pre lang ="cs">受保护的无效btnSubmit_Click(对象发送者,EventArgs e)
{
//一些服务器端操作
TextBox1.Text =&"voila& quot ;;
}</pre>
"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if (document.getElementById("<%=txtName.ClientID%>").value!="")||(document.getElementById("<%=txtEmail.ClientID %>").value!="")||(document.getElementById("<%=txtWebURL.ClientID %>").value!="")||(document.getElementById("<%=txtZIP.ClientID%>").value!="")
{
if (document.getElementById("<%=txtName.ClientID%>").value!="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
else if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
else if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
else if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
else if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
else if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
return true;
}
if (document.getElementById("<%=txtName.ClientID%>").value=="")&&(document.getElementById("<%=txtEmail.ClientID %>").value=="")&&(document.getElementById("<%=txtWebURL.ClientID %>").value=="")&&(document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
return true;
}
}
</script>
<br />
<br />
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="Name"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Web URL"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txtWebURL" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server"
Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html></pre>
//btnSubmit_Click event which will do server side activity after //validating the page
<pre lang="cs">protected void btnSubmit_Click(object sender, EventArgs e)
{
//some server side action
TextBox1.Text = "voila";
}</pre>
从javascript启用验证程序控件,或者使用一些自定义的javascript方法来验证表单.
Enable validator controls from javascript or instead use some custom javascript methods to validate the form.
这篇关于验证控制的条件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!