我使用jquery.validate.js进行字段验证。根据.js文件中相应字段的声明,其显示错误消息。
我将此代码用于电子邮件字段的自定义验证。
<script type="text/javascript">
$().ready(function() {
$("#formcustomplan").validate({
email: {
required: true,
email: true
},
messages: {
email: "Email is required in valid format"
}
});
});
</script>
它的显示消息类似“需要使用有效格式的电子邮件”,但是我希望该错误消息获取电子邮件ID并在错误消息中显示。(例如,如果我在电子邮件框中输入test.com。它应该在错误消息中显示“ test.com不是有效的电子邮件”)
您可以使用以下方法重置默认电子邮件验证消息:
$.validator.messages.email = "Your custom message for email field"
在您的情况下,您可以在用户输入中将新值添加到自定义消息中。这是使用的技巧keyup
:
$('.email').keyup(function () {
$.validator.messages.email = $('.email').val() + ' is not valid email';
});
$("#formcustomplan").validate({
email: {
required: true,
email: true
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句