Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to perform required field validation on specific condition..
example: suppose I have 1 dropdown, 2 textbox and 1 submit button

if dropdown selectvalue is "1" then 1textbox should be perform required field validation on submit button click or
otherwise 2textbox perform validation.
Posted
Comments
Karthik_Mahalingam 9-Jan-14 4:39am    
c# or javascript ?

try this sample.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
     
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    <asp:DropDownList ID="ddl" runat="server">
        
        <asp:ListItem Text="1" />
        <asp:ListItem Text="2" />
    </asp:DropDownList>
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </form>
</body>
</html>




code behind:


C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              lblMessage.Text = "";
          }
      }



      protected void btnSubmit_Click(object sender, EventArgs e)
      {

          if (ddl.Text == "1")
          {
              if (TextBox1.Text == "")
              {
                  lblMessage.Text = "please enter TextBox1 text";
                  return;
              }
          }
          else if (ddl.Text == "2")
          {
              if (TextBox2.Text == "")
              {
                  lblMessage.Text = "please enter TextBox2 text";
                  return;
              }
          }
          lblMessage.Text = "success";


      }
 
Share this answer
 
Comments
GANESHGITE 9-Jan-14 4:55am    
Thank for giving response..
Actually my page contain more than 20 field.Above example is the only demo example.
I think its not proper way for actual scenario..
can you give any other solution..
Karthik_Mahalingam 9-Jan-14 5:02am    
can u pls provide more info so that i can reply based on that...
What i understood is as follows
You have two textboxes to be validated on custom provided conditions.

If DropDown.SelectedIndex = 1 Then Validate only TextBox1
OTHERWISE
Validate both TextBoxes

Solution1:
On SelectedIndexChange of DropDown =>
Check IF[SelectedIndex = 1] THEN disable TextBox2 validator ELSE Enable TextBox2 validator


Solution2:
Use Custom validator as used in this link[^]
 
Share this answer
 
try this javascript function:-

XML
function validate() {
         if (document.getElementById("<%= ddlSlct.ClientID%> ").value == "1") {
             if (document.getElementById("<%= taxbox1.ClientID%> ").value != "") {
                 alert("textbox1 is empty");
             }
             else {
             if (document.getElementById("<%= taxbox2.ClientID%> ").value != "") {
                 alert("textbox2 is empty");
             }
         }
    }


<asp:Button ID="btnSubmit" runat="server" OnClientClick="validate();" />


make changes in function as per your validation.
i have just check if it is empty or not.
 
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