2014-11-05 127 views
1

我需要在歐芹遠程中使用兩個輸入字段進行驗證。即歐芹遠程多輸入值驗證

$('#user-name').parsley().addAsyncValidator('remotevalidator',remotevalidationfunction,'validation-url.htm'); 

上述示例取得用戶名的值,並將其作爲參數傳遞給validation-url.htm。 我需要傳遞另一個輸入字段的值location

請讓我知道這可以在歐芹遠程完成。

回答

2

我不認爲你可以做到這一點與遠程驗證。解決問題的最佳方法是使用$.ajaxaddValidator

<input type="text" name="username" data-parsley-username /> 
<input type="text" name="location" id="location" /> 

<script> 
window.ParsleyValidator 
.addValidator('username', function (value, requirement) { 
    var response = false, 
     location = $("#location").val();  

    $.ajax({ 
     url: "validation-url.htm", 
     data: {username: value, location: location}, 
     dataType: 'json', 
     type: 'get', 
     async: false, 
     success: function(data) { 
      // if you send something from the server, you might want to 
      // do some verification here 
      response = true; 
     }, 
     error: function() { 
      response = false; 
     } 
    }); 

    return response; 
}, 32) 
.addMessage('en', 'username', 'Username is invalid.'); 
</script>