jQuery插件【validate】国际化校验插件
// 引入jQuery库
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
// 引入jQuery Validate插件
<script src="https://cdn.jsdelivr.net/jquery.validation/1.19.2/jquery.validate.min.js"></script>
// 引入jQuery Validate国际化消息文件,假设是中文
<script src="https://cdn.jsdelivr.net/jquery.validation/1.19.2/localization/messages_zh.min.js"></script>
<script>
$(document).ready(function() {
$("#myform").validate({
rules: {
firstname: "required",
lastname: "required",
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true
},
"topic[]": {
required: "#newsletter:checked",
minlength: 1
}
},
messages: {
firstname: "请输入您的名字",
lastname: "请输入您的姓氏",
username: {
required: "请输入用户名",
minlength: "用户名至少需要两个字符"
},
password: {
required: "请输入密码",
minlength: "密码至少需要5个字符"
},
confirm_password: {
required: "请输入确认密码",
minlength: "确认密码至少需要5个字符",
equalTo: "两次输入密码不一致"
},
email: "请输入一个有效的电子邮件地址",
"topic[]": {
required: "请至少选择一个兴趣话题",
minlength: "请选择一个兴趣话题"
}
}
});
});
</script>
这个代码实例展示了如何使用jQuery Validate插件进行表单验证,并结合国际化消息文件提供中文的错误提示信息。代码中包含了常见的验证规则和错误消息的定义,并且使用了中文作为提示语言。
评论已关闭