Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 06, 2012 02:26 PM by karinos57
Member
24 Points
108 Posts
Nov 06, 2012 01:01 AM|LINK
i have 2 fields and i need to make one of the fields a required fields only if the other field has content of "NO" value in it. thanks
All-Star
31334 Points
5414 Posts
Nov 06, 2012 03:34 AM|LINK
hi, here is a sample
<script> function Validate(source, args) { if (document.getElementById('<%= TextBox1.ClientID %>').value.toUpperCase() == "NO" && document.getElementById('<%= TextBox2.ClientID %>').value.length == 0) args.IsValid = false; else args.IsValid = true; } </script> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TextBox2 Should Be Filled" Display="Dynamic" ClientValidationFunction="Validate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> <asp:Button ID="Button1" runat="server" Text="Button" /> </form> </body> </html> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Page.Validate(); if (!Page.IsValid) Response.Write("TextBox2 should be filled"); } } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = !(this.TextBox1.Text.Equals("NO", StringComparison.CurrentCultureIgnoreCase) && this.TextBox2.Text.Length == 0); }
Nov 06, 2012 02:26 PM|LINK
thank you so much but i am trying to understand your code first so i can try it. can you explain what is the:
.getElementById
My 2 fields are in a detailview control. thanks
karinos57
Member
24 Points
108 Posts
How do i validate a field based on the content of another field
Nov 06, 2012 01:01 AM|LINK
i have 2 fields and i need to make one of the fields a required fields only if the other field has content of "NO" value in it. thanks
karthicks
All-Star
31334 Points
5414 Posts
Re: How do i validate a field based on the content of another field
Nov 06, 2012 03:34 AM|LINK
hi, here is a sample
<script> function Validate(source, args) { if (document.getElementById('<%= TextBox1.ClientID %>').value.toUpperCase() == "NO" && document.getElementById('<%= TextBox2.ClientID %>').value.length == 0) args.IsValid = false; else args.IsValid = true; } </script> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TextBox2 Should Be Filled" Display="Dynamic" ClientValidationFunction="Validate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> <asp:Button ID="Button1" runat="server" Text="Button" /> </form> </body> </html> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Page.Validate(); if (!Page.IsValid) Response.Write("TextBox2 should be filled"); } } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = !(this.TextBox1.Text.Equals("NO", StringComparison.CurrentCultureIgnoreCase) && this.TextBox2.Text.Length == 0); }Karthick S
karinos57
Member
24 Points
108 Posts
Re: How do i validate a field based on the content of another field
Nov 06, 2012 02:26 PM|LINK
thank you so much but i am trying to understand your code first so i can try it. can you explain what is the:
My 2 fields are in a detailview control. thanks