如果墨西哥是真的,我需要验证邮政编码。
我有一个表格,接下来您可以看到代码,我不明白为什么不考虑验证,这是怎么回事?
我粘贴了找到的示例代码
https://www.ibm.com/support/knowledgecenter/it/SSVRGU_9.0.1/basic/H_SEARCH_METHOD.html
// on js
Parsley.addValidator('zip', {
validateString: function (value) {
// Zippopotam.us returns a status 404 for incorrect zip codes,
// so we simply return the ajax request:
return $.ajax({
url: '//www.zippopotam.us/mx/' + value,
success: function (result, success) {
$('#zipState').val(result['places'][0]['state abbreviation']);
},
error: function (result, success) {
$('#zipState').val('');
}
})
}
});
//input in form
<div class="consumidor_cp_right">
<div class="consumidor_cp">
<label for="zip"><span>*</span>C.P.</label>
<input type="text" class="input_control" name="zip" id="cp" data-parsley-length="[5,5]" required="" data-parsley-zip="mx">
</div>
</div>
但是验证尚未发生。我认为这是我在输入中调用验证的方式,但是我不确定应该确切地称呼它还是对其进行引用。
答案 0 :(得分:0)
它对我有效,
请参阅下面的完整代码,
<h1>Custom validators</h1>
<h3>Simple ajax request</h3>
<form data-parsley-validate="">
<input type="text" class="input_control" name="zip" id="cp" data-parsley-length="[5,5]" required="" data-parsley-zip="mx">
<br>
<input type="submit" value="validate">
</form>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="parsley.min.js"></script>
<script type="text/javascript">
Parsley.addValidator('zip', {
validateString: function(value) {
// Zippopotam.us returns a status 404 for incorrect zip codes,
// so we simply return the ajax request:
return $.ajax({
url: '//www.zippopotam.us/mx/' + value,
success: function (result, success) {
alert(result['places'][0]['state abbreviation']);
},
error: function (result, success) {
alert('Not found');
}
})
},
messages: {en: 'There is no such zip for the country "%s"'}
});
Parsley.on('form:submit', function() {
return false; // Don't submit form for this demo
});
</script>
检查是否包含您的欧芹js,并确保您不使用jQuery的苗条版本,因为它不支持ajax。