1

I am new to ASP.NET.

I have three ASP controls: textbox, dropdown and submit button.

If the dropdown is selected, the textbox must be a required field and if dropdown is not selected textbox should not be required field. The challenge right now is that my required field validator fires even if dropdown is not selected.

I'm using JavaScript to check if textbox is null and disable my required field.

<td><label for="schoolName">SCHOOLNAMES</label></td>
   <td><asp:TextBox ID="txtschoolname" runat="server"></asp:TextBox>
       <asp:RequiredFieldValidator ID="RequiredFieldValidatorSchoolName"            runat="server" 
        ControlToValidate="txtschoolname" ForeColor="Red"   
       ErrorMessage="Required"></asp:RequiredFieldValidator>
   </td>
<td>Bank Name</td>
        <td>
          <select">
              <option>Please select the bank</option>
              <option value="DBN">DBN</option>
              <option value="CCC">CCC</option>
          </select>
        </td>

<td colspan="2">
        <asp:Button ID="Button1" runat="server" Text="submit"  
            OnClientClick=" validate();" onclick="Button1_Click"   />

JavaScript:

function validate() {
    var txt = document.getElementById("txtschoolname");
    alert(txt);
    var ddlObj = document.getElementById("<%=txtschoolname.ClientID%>");
    var validatorObject = document.getElementById("<%=RequiredFieldValidatorSchoolName.ClientID%>");

    alert(ddlObj);
    if (txt == null) {
        validatorObject.enabled = false;
        //  validatorObject.isvalid = true;
    }
}

2 Answers 2

1

Check out this resource: https://msdn.microsoft.com/en-us/library/Aa479045.aspx

Take a look at the client side API's section, which indicates using the ValidatorEnable method to enable or disable the validator:

ValidatorEnable('<%= RequiredFieldValidatorSchoolName.ClientID %>', false); //disable
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply im still stuck .Please assist i call the validatorEnable ,,,,please code below
0

You can do this with javascript,

An exact question as been answered, Validation with checkbox, and it also caters for javsacript being disbaled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.