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;
}
}