jQuery Validate插件
jQuery Validate是jQuery库的一个插件,用来验证表单,用起来非常方便,只需要在页面的head标签中引入以下代码即可。
<script src="https://cdn.staticfile.org/jquery/3.5.0/jquery.min.js"></script> <script src="https://cdn.staticfile.org/jquery-validate/1.9.0/jquery.validate.min.js"></script> <script src="https://cdn.staticfile.org/jquery-validate/1.9.0/localization/messages_cn.js"></script> <script> $().ready(function() { $("#commentForm").validate({//此处commenForm为表单的ID名称 submitHandler: function(form) { form.submit();//一定要写这个,验证完之后才会提交 } }); }); </script> <style> .error{ color:red; } </style>
注意:如果在使用过程中中该插件并不生效,可能是当前Html页面中应用的js文件太多,先后顺序导致该插件不起作用,可尝试调换下位置。
本文参考:https://www.runoob.com/jquery/jquery-plugin-validate.html

浙公网安备 33010602011771号