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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

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.