Reputation: 2227
Im struggling getting this to work the way i need. I have two RequiredFieldValidators and two textboxes (Side note: although i have Javascript below i dont mind doing this in another way. I did try code behind but realised validation didnt kick in until i clicked a button twice):
<asp:TextBox ID="EmailTextbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailR" runat="server" ErrorMessage="Email" ControlToValidate="EmailTextbox" ></asp:RequiredFieldValidator>
<asp:TextBox ID="NameTextbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="NameR" runat="server" ErrorMessage="Enter your name" ControlToValidate="NameTextbox" ></asp:RequiredFieldValidator>
I then have some script
<script type="text/javascript">
$(document).ready(function () {
$('#<%=EmailTextbox.ClientID%>').keyup(function () {
if ($(this).val() != '') {
ValidatorEnable(document.getElementById('<%= NameR.ClientID%>'), true);
}
else
ValidatorEnable(document.getElementById('<%= NameR.ClientID%>'), false);
});
});
</script>
What im trying to do is:
If EmailTextbox has an email then disable NameTextbox validation. If EmailTextbox has NO email then enable NameTextbox validation and disable EmailTextbox validation.
With me being pretty new to JQuery/Javascript i have tried several attempts in trying to achieve the above however reading more into it, theres a possibility that i could have the wrong JQuery file (that said with this being an existing project i havent really added any ref to any JQuery so it could well be that i have the code right but need a ref to a JQuery or need to include a new version).
Overall if i can
Thanks
Upvotes: 1
Views: 145
Reputation: 336
You can try it
$(document).ready(function () {
$('#<%=EmailTextBox.ClientID%>').keyup(function () {
if ($(this).val() != null && $(this).val().length != 0) {
$('#<%= NameRequiredFieldValidator.ClientID%>').hide();
}
else {
$('#<%= NameRequiredFieldValidator.ClientID%>').show();
$('#<%= EmailRequiredFieldValidator.ClientID%>').hide();
}
});
Upvotes: 1
Reputation: 64
You can try this, similar to what you had.
function doSomething()
{
var myVal = document.getElementById('myValidatorClientID');
ValidatorEnable(myVal, false);
}
Or, you could use the visible=true/false on them which renders them inactive (meaning set visible property from code behind).. This might cost you an ajax trip to the code behind using scripmanager and __doPostBack in order to call a server-side function that can than process your logic... A lot of developers don't realize that at least in webforms, you can call your code behind methods from JS, just be very careful - as each call back can get costly...
A good article on communicating from ("front end to code behind via JS") - http://www.codedigest.com/Articles/ASPNET/320_Doing_or_Raising_Postback_using___doPostBack()_function_from_Javascript_in_AspNet.aspx
Hope that helps or get's you back on the right track!!!
Upvotes: 0
Reputation: 715
In your code you make the validation enable wrongly when a email value was not null disable validation on name and enable for email else viceversa
<script type="text/javascript">
$(document).ready(function () {
$('#<%=EmailTextbox.ClientID%>').keyup(function () {
if ($.trim($(this).val()).length)
ValidatorEnable(document.getElementById('<%= NameR.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%= EmailTextbox.ClientID%>'), true);
}
else
{
ValidatorEnable(document.getElementById('<%= NameR.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%= EmailTextbox.ClientID%>'), false);
}
});
});
</script>
Upvotes: 0