-1

I want to change the required message for the elements but I don't know why my script doesn't work! Are the src's correct?!

<head>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery-1.10.2.min.js"></script>

<script>
    $('#form1 input[type=text]').on('change invalid', function () {
        var textfield = $(this).get(0);

        // 'setCustomValidity not only sets the message, but also marks
        // the field as invalid. In order to see whether the field really is
        // invalid, we have to remove the message first
        textfield.setCustomValidity('');

        if (!textfield.validity.valid) {
            textfield.setCustomValidity('Some message... ');
        }
    });
</script>

And here is the body.

    <form id="form1" class="form-basic" action="~/MortgageAndRent/InsertMortgageAndRent/" method="post" >
        <input type="hidden" name="UserId" value="@Model.UserId" />

        <div class="form-row">
            <label>
                <span>Province</span>
                <input type="text" name="AddressViewModel.Province" value="@Model.AddressViewModel.Province" required />
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>City</span>
                <input type="text" name="AddressViewModel.City" value="@Model.AddressViewModel.City" required />
            </label>
        </div>

. . .

  • You have to learn how to debug your code first. Check this link http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code – Aman Rawat Jan 08 '17 at 08:52
  • Your `#form2 input[type=text]` element doesn't exist when you're running your code setting up the event handler. See the linked question's answers. (You'll also need an element with `id="form2"` around the input, or to change the selector.) – T.J. Crowder Jan 08 '17 at 08:55

1 Answers1

-2

This $('#form2 input[type=text]').on('change invalid', function () { var textfield = $(this).get(0);}); you had forgotten }; but in your html you are not declaring the id of form2 so i think this $('input[type=text]').on('change invalid', function () { var textfield = $(this).get(0);});

NotanNimda
  • 407
  • 1
  • 3
  • 11