Reputation: 11
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;
}
}
Upvotes: 1
Views: 1644
Reputation: 5260
You can do this with javascript,
An exact question as been answered, Validation with checkbox, and it also caters for javsacript being disbaled.
Upvotes: 0
Reputation: 50728
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
Upvotes: 1