最简单的JQuery验证规则示例

编程入门 行业动态 更新时间:2024-10-08 04:35:18
本文介绍了最简单的JQuery验证规则示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下HTML表单成功使用了jQuery的表单验证,如果留空则在表单字段的右侧显示此字段是必需的,如果输入的字符少于2个则显示请输入至少2个字符。但是,我不想使用cname表单输入字段中的class和minlength属性指定验证元数据,而是使用jQuery的规则API,其中规则在validate函数的主体中指定。在此先感谢:

The following HTML form successfully utilizes jQuery's form validation, displaying "This field is required" to the right of the form field if left blank, and "Please enter at least 2 characters" if fewer than 2 characters were entered. However, instead of the validation metadata being specified using the class and minlength attributes on the "cname" form input field, I'd like to use jQuery's "rules" API instead, where the rules are specified in the body of the validate function. Thanks in advance:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3/TR/html4/loose.dtd"> <html> <head> <script src="/lib/js/jquery.js"></script> <script src="/lib/js/jquery.validate.js"></script> <script> $(document).ready(function(){$("#commentForm").validate( /* rules/messages here */ );} ); </script> </head> <body> <form id="commentForm" method="get" action=""> <fieldset> <legend>A simple comment form with submit validation and default messages</legend> <p> <label for="cname">Name</label> <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" /> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> </fieldset> </form> </body> </html>

推荐答案

这篇博客文章可以解决问题。

更多推荐

最简单的JQuery验证规则示例

本文发布于:2023-11-28 08:05:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   示例   规则   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!