1

I have two textboxes in my asp.net application. I want to make txtContact a mandatory field if user enters something in txtName using JavaScript.

Here is my code,

<asp:TextBox ID="txtName" runat="server" CssClass="selectstyle" onkeypress="javascript:AvoidEnter();" MaxLength="10" ValidationGroup="CreateUser" />
<asp:TextBox ID="txtContact" runat="server" CssClass="selectstyle" onkeypress="javascript:AvoidEnter();" MaxLength="20" />
<asp:RequiredFieldValidator ErrorMessage="Please enter contact info" ClientIDMode="Static" ID="rfvContact" Enabled="false" ValidationGroup="CreateUser" ControlToValidate="txtContact" runat="server" />


<script type="text/javascript">
    $(function () {
        $('#<%= txtName.ClientID %>').blur(function () {
            if ($.trim($('#<%= txtName.ClientID %>').val()) != '') {
                //enable container number validator
                ValidatorEnable(document.getElementById('<%=rfvContact.ClientID%>'), true);
            }
            else {
                //disable container number validator
                ValidatorEnable(document.getElementById('<%=rfvContact.ClientID%>'), false);
            }
        });

    });
</script>

The problem with above script is, it is working completely fine in Chrome browser but the validation is not working in IE 11.

Any other alternative please?

nrvbha
  • 139
  • 4
  • 19
  • There is a similar question here. http://stackoverflow.com/questions/9746815/asp-net-validation-for-multiple-text-boxes – causita Feb 12 '15 at 16:50

1 Answers1

0

From my test, I also failed to use "onkeypress" for my code...However, I just to use another to replace this function. First of all, create new function:

$(document).ready(function () {
    $('#txtContact').change(keypressfunction);  });

Then, create function "keypressfunction":

function keypressfunction()
{
    var txtMessage = document.getElementById("txtName").value;
    if (txtMessage == '') {
        var contactField = $("#txtContact").val();
        if (contactField != '') {
            $('#txtName').val(contactField);
        } 
    }
}

This is different from my code and I just add substr to get before character. You may try to test this code and fix it.

jeremylee
  • 21
  • 1
  • 3