2015-10-31 246 views
7

我有一個電子郵件字段的更改事件,該電子郵件字段應該使用ajax調用來驗證電子郵件地址是唯一的。問題是它告訴我該元素不支持setCustomValidity方法。請幫忙。element.setCustomValidity似乎沒有工作

$("#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) } 
     } 
    ); 

}) 

回答

5

setCustomValidity是DOM API而不是jQuery API。它也需要是符合HTML5的瀏覽器和表單。

var element = $("#tbEmail")[0]; 
element.setCustomValidity('The email address entered is already registerd.'); 
+0

這工作!非常感謝你vinayakj。我會在那玩幾天而沒有想到這一點。我如何接受答案?我是新的stackoverflow。 – pachyderm94

0

使用get(0)

var element = $("#tbEmail").get(0); 
    element.setCustomValidity('The email address entered is already registerd.'); 
0

更簡單的方法來此,將 「[0]」:

$("#tbEmail")[0].setCustomValidity("")