验证控制的条件触发 [英] Conditional firing of validational control

查看:84
本文介绍了验证控制的条件触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的网络表单中有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&quot; & 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">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;WebApplication1._Default&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head runat=&quot;server&quot;&gt;
&lt;title&gt;Untitled Page&lt;/title&gt;
&lt;style type=&quot;text/css&quot;&gt;
.style1
{
width: 27%;
height: 233px;
}
.style2
{
width: 68px;
}
.style3
{
width: 208px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
&lt;div&gt;
&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
function validate()
{
var emailPat = /^(\&quot;.*\&quot;|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value;
var matchArray = emailid.match(emailPat);
var Url=&quot;^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&amp;\?\/.=]+$&quot;
var tempURL=document.getElementById(&quot;&lt;%=txtWebURL.ClientID%&gt;&quot;).value;
var matchURL=tempURL.match(Url);
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value!=&quot;&quot;)
{
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value!=&quot;&quot;)
{
alert(&quot;Name Feild can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).focus();
return false;
}
else if(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Email id can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).focus();
return false;
}
else if (matchArray == null)
{
alert(&quot;Your email address seems incorrect. Please try again.&quot;);
document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).focus();
return false;
}
else if(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Web URL can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value=&quot;http://&quot;
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).focus();
return false;
}
else if(matchURL==null)
{
alert(&quot;Web URL does not look valid&quot;);
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).focus();
return false;
}
else if (document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Zip Code is not valid&quot;);
document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).focus();
return false;
}
return true;
}
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value==&quot;&quot;)
{
return true;
}
}
&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table class=&quot;style1&quot;&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label4&quot; runat=&quot;server&quot; Text=&quot;Name&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtName&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text=&quot;Age&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtEmail&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text=&quot;Web URL&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtWebURL&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&amp;nbsp;&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&amp;nbsp;&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:Button ID=&quot;btnSubmit&quot; OnClientClick=&quot; return validate()&quot; runat=&quot;server&quot;
Text=&quot;Submit&quot; 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 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 = &quot;voila&quot;;
}</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(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value;
var matchArray = emailid.match(emailPat);
var Url=&quot;^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&amp;\?\/.=]+


& 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>
&quot;
var tempURL=document.getElementById(&quot;&lt;%=txtWebURL.ClientID%&gt;&quot;).value;
var matchURL=tempURL.match(Url);
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value!=&quot;&quot;)||(document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value!=&quot;&quot;)
{
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value!=&quot;&quot;)
{
alert(&quot;Name Feild can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).focus();
return false;
}
else if(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Email id can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).focus();
return false;
}
else if (matchArray == null)
{
alert(&quot;Your email address seems incorrect. Please try again.&quot;);
document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).focus();
return false;
}
else if(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Web URL can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value=&quot;http://&quot;
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).focus();
return false;
}
else if(matchURL==null)
{
alert(&quot;Web URL does not look valid&quot;);
document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).focus();
return false;
}
else if (document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value==&quot;&quot;)
{
alert(&quot;Zip Code is not valid&quot;);
document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).focus();
return false;
}
return true;
}
if (document.getElementById(&quot;&lt;%=txtName.ClientID%&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtEmail.ClientID %&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtWebURL.ClientID %&gt;&quot;).value==&quot;&quot;)&amp;&amp;(document.getElementById(&quot;&lt;%=txtZIP.ClientID%&gt;&quot;).value==&quot;&quot;)
{
return true;
}
}
&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table class=&quot;style1&quot;&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label4&quot; runat=&quot;server&quot; Text=&quot;Name&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtName&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text=&quot;Age&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtEmail&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text=&quot;Web URL&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;txtWebURL&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&amp;nbsp;&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;style2&quot;&gt;
&amp;nbsp;&lt;/td&gt;
&lt;td class=&quot;style3&quot;&gt;
&lt;asp:Button ID=&quot;btnSubmit&quot; OnClientClick=&quot; return validate()&quot; runat=&quot;server&quot;
Text=&quot;Submit&quot; 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 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 = &quot;voila&quot;;
}</pre>


从javascript启用验证程序控件,或者使用一些自定义的javascript方法来验证表单.
Enable validator controls from javascript or instead use some custom javascript methods to validate the form.


这篇关于验证控制的条件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆