gpt4 book ai didi

javascript - 最简单的 JQuery 验证规则示例

转载 作者:可可西里 更新时间:2023-11-01 01:23:06 25 4
gpt4 key购买 nike

下面的 HTML 表单成功地利用了 jQuery 的表单验证,如果留空则在表单字段右侧显示“此字段为必填项”,如果输入的字符少于 2 个则显示“请输入至少 2 个字符”。但是,我不想使用“cname”表单输入字段上的类和最小长度属性指定验证元数据,而是使用 jQuery 的“规则”API,其中规则在验证函数的主体中指定。提前致谢:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/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>

最佳答案

rules: {
cname: {
required: true,
minlength: 2
}
},
messages: {
cname: {
required: "<li>Please enter a name.</li>",
minlength: "<li>Your name is not long enough.</li>"
}
}

关于javascript - 最简单的 JQuery 验证规则示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com