Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Prasad Avunoori 10-Jun-14 1:48am    
Why don't you try javascript?
What have you tried?
amritha444 10-Jun-14 2:11am    
<asp:TextBox ID="txtspousename" CausesValidation="true" runat="server" Width="260px">
<asp:customvalidator ID="Customvalidator1" EnableClientScript="false" Display="Dynamic" ValidationGroup="v" onservervalidate="Customvalidator1_ServerValidate" forecolor="Red" errormessage="*" controltovalidate="txtspousename" runat="server">

server side
protected void Customvalidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (chkweddingbased.Checked == true)
{
if (txtspousename.Text=="")
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
else
{
args.IsValid = true;
}
}



its not firing while textbox is null..
Prasad Avunoori 10-Jun-14 2:13am    
Hi Ammu,

Please view below my solution. Comment if you need any help.

Html:

XML
<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>
 
Share this answer
 
Comments
amritha444 10-Jun-14 3:49am    
Thanks all for your response
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.NET
<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);
 
Share this answer
 
v2
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900