与验证相关 [英] Related with validation
本文介绍了与验证相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我有三个字段的名称,地址,婚姻状况,配偶..我想对所有这些字段进行验证..问题是验证配偶应该只在我检查Ismarried复选框时出现..我使用自定义验证器但它没有解雇..任何人都可以为这个问题提供解决方案
在此先感谢
Amritha
Hi All
I have three fields name,address,marital status,spouse ..i want to give validation for all these fields .. problem is validation for spouse should appear only when i checked Ismarried check box .. i used custom validator but its not firing .. can any one give solution for this question
Thanks in Advance
Amritha
推荐答案
Html:
Html:
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> <asp:CheckBox ID="chkIsMarried"
runat="server" /> <asp:TextBox ID="txtSpouse" runat="server"></asp:TextBox><asp:Button
ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
</div>
Javascript:
<script type="text/javascript">
function validate() {
debugger;
var name, address, isMarried, spouse;
name = document.getElementById("txtName").value;
address = document.getElementById("txtAddress").value;
isMarried = document.getElementById("chkIsMarried").checked;
spouse = document.getElementById("txtSpouse").value;
if (name == "") {
alert("Name requered");
document.getElementById("txtName").focus();
return false;
}
else if (address == "") {
alert("Address requered");
document.getElementById("txtAddress").focus();
return false;
}
else if (isMarried==true) {
if (spouse == "") {
alert("Spouse name requered when you are married");
document.getElementById("txtSpouse").focus();
return false;
}
}
}
</script>
你可以使用服务器端和客户端:
服务器端:禁用加载功能中的所有验证,并在检查复选框控件并启用它时启用它按钮点击
客户端:相同但使用javascript
HTML:
you can use server side and client side:
server side: disabled all validation in load function and enabled it when check the checkbox control and validate it by button click
client side : same but using javascript
HTML:
<asp:textbox id="txtName" runat="server" xmlns:asp="#unknown" />
<asp:requiredfieldvalidator id="RequiredFieldValidator4" runat="server" controltovalidate="txtName" display="None" errormessage="Please enter name">
ValidationGroup="info"
<asp:checkbox id="chkMarried" runat="server" onchange="enableddisabled(this);" />
<asp:button id="btnSubmit" runat="server" validationgroup="info"/>
and use ValidatorEnabled function to enable and disable the validation control in javascript
ValidatorEnabled(validationcontrolname,boolean);
为了使用自定义验证器,您需要一个RequiredFieldValidator来进行相同的控制。 />
将RequiredFieldValidator放入文本框(txtspousename)并尝试。
希望它会f是你的Serverside活动。
此解决方案解决了我的问题
Inorder to use Custom validator, you need a RequiredFieldValidator for same control.
Put the RequiredFieldValidator for your textbox(txtspousename) and try.
Hope it will fire your Serverside event.
This solution resolved my problem
这篇关于与验证相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文