Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

element.setCustomValidity does not seem to work

I have a change event on an email field that is supposed to validate that the email address is unique using an ajax call. The problem is that it is telling me that the element does not support the setCustomValidity method. Please Help.

$("#tbEmail").change(function (event) {
    var obj = new Object();

    obj.email = $("#tbEmail").val();
    var params = JSON.stringify(obj);

    $.ajax
    (
        {
            type: "POST",
            url: "./controllers/Consumer.aspx/validateEmail",
            data: params,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                if (result.d != 0) {

                    var element = $("#tbEmail");
                    element.setCustomValidity('The email address entered is already registerd.');
                    alert(element.checkValidity());
                    alert(element.validationMessage);
                }
                else {
                    $("#tbEmail").setCustomValidity("");
                };
            },
            error: function (result) { ProcessError(result) }
        }
    );

})
like image 806
pachyderm94 Avatar asked Oct 31 '15 20:10

pachyderm94


3 Answers

setCustomValidity is DOM API not jQuery API. In your code you are trying to call it on jQuery instance. So you need to first get the DOM element instance & then call the API.

Also it needs to be HTML5 compliant browser and a form.

 var element = $("#tbEmail")[0];
 element.setCustomValidity('The email address entered is already registerd.');
like image 126
vinayakj Avatar answered Oct 20 '22 05:10

vinayakj


A simpler approach to this, adding "[0]":

$("#tbEmail")[0].setCustomValidity("")
like image 7
Paulo Henrique Avatar answered Oct 20 '22 06:10

Paulo Henrique


use get(0).

    var element = $("#tbEmail").get(0);
    element.setCustomValidity('The email address entered is already registerd.');
like image 5
AmirHossein Manian Avatar answered Oct 20 '22 05:10

AmirHossein Manian