jquery中的validate plugin的问题

Landor2004 2008-05-04 09:48:31
validate的验证似乎针对表单元素的name属性为a.b的不起作用,不知道是不是插件的bug,例子如下
<html><head>
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
$(function(){
$("form").validate({
rules: {
test: {
required: true,
minlength: 3,
remote: "xxx.jsp",
}
},
messages: {
test: {
required: "test不能为空",
minlength: jQuery.format("帐号至少 {0} 位"),
remote: jQuery.format("{0} 已经存在")
}
},
submitHandler: function(form) {
$(form).ajaxSubmit(saveOptions);
}
});
});
</script>

</head><body>
<form>
<input name="test" id="test" />
</form>
</body></html>


其中更改为<input name="test.a" id="test" />,validate.js的源码中似乎是根据name来验证,这样test.a形式的name就不支持了

如果不支持以上格式,那么在struts等框架的表单应用中会很不爽,比如用户名我们会这样写

<ss:textfield id="userCode" name="user.code"/>,这样写的话,jquery的validate验证不起作用

不知道大家是否遇到了这个问题的,怎么处理的
...全文
1047 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Landor2004 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 woniu02154 的回复:]
引用楼主 Landor2004 的帖子:
$("form").validate({
rules: {
test: {
required: true,
minlength: 3,
remote: "xxx.jsp",
}
},
messages: {
test: {
required: "test不能为空",
minlength: jQuery.format("帐号至少 {0} 位"),
remote: jQuery.format("{0} 已经存在")
}
},

请问一下,楼主:
你这里的“minlength: jQuery.format("帐号至少 {0} 位"),”
能确实管用吗?我怎么试了好多次都是它默认的“Please…
[/Quote]
不好意思,结贴了之后就没来看过,我没遇到你的问题
woniu02154 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 Landor2004 的帖子:]
$("form").validate({
rules: {
test: {
required: true,
minlength: 3,
remote: "xxx.jsp",
}
},
messages: {
test: {
required: "test不能为空",
minlength: jQuery.format("帐号至少 {0} 位"),
remote: jQuery.format("{0} 已经存在")
}
},
[/Quote]
请问一下,楼主:
你这里的“minlength: jQuery.format("帐号至少 {0} 位"),”
能确实管用吗?我怎么试了好多次都是它默认的“Please enter at least 3 characters.”呢?

你有没有碰到这个问题?

我的QQ:176040316
gyhgc 2008-05-27
  • 打赏
  • 举报
回复
我有一个问题请教大家。
我用Ajax提交验证是这样写的
$("#duty_name").formValidator({onshow:"请输入职务名称",onfocus:"职务名称不能为空",oncorrect:"职务名称合法"}).InputValidator({min:1,max:50,onerror:"职务名称不能为空,请确认"})
.AjaxValidator({
type : "get",
url : encodeURI(encodeURI("../maintain/duty_checkname.action?top_org_id="+document.getElementById("top_org_id").value+"&name="+$("#duty_name").val())),
datatype : "json",
success : function(data){
if( data == "1" )
{
return false;
}
else
{
return true;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "该用户名不可用,请更换用户名",
onwait : "正在对用户名进行合法性校验,请稍候..."
}).DefaultPassed();

<td><s:textfield name="duty.duty_name" id="duty_name" /><div id="duty_nameTip" style="width:250px"></div></td>

我每次向aciton提交时发现duty_name的值没有根据输入不同的值而发生变化,请问是为什么呢?
Landor2004 2008-05-06
  • 打赏
  • 举报
回复
解决了,还应该去看他的文档啊,http://docs.jquery.com/Plugins/Validation/Validator


只需要这样写

rules: {
“test.a": {
required: true,
minlength: 3,

Landor2004 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sl514 的回复:]
这个问题我也有遇到过  但是我要说的 是   首先jQuery的取值 基本上是用id 也就是$("#id") 基本上不用name 除非是tagName

其次我用过 id="user.name" 好像有个jQuery高手告诉我 $("#user\\.name") 是可以娶到的

你说的name="user.code" 应该是struts2的方法   你可以这样做remote: "xxx.jsp?"+$("#test").val(),

如果上面的方法不行 那你就得考虑在你的action里 设个新字段来接受他   如果还不行 就加我QQ 624767717 我…
[/Quote]
多谢楼上的朋友的帮助

你说的formValidator是另一个validate插件,我研究一下

我知道jQuery的取值基本上是用id,否则我也不能这么写id="userCode" name="user.code",即使我不写id,struts2也不会自动生成和name一样的id,但是validate是byName的

还有我知道你说的$("#user\\.name")方式可以取到值,但是和validate插件似乎没有联系,因为他的rule是根据name来自定义的

你的意思是用参数把要验证的字段传回action吧,这个我知道,呵呵,ajax的验证就不用说了,仅从前台验证部分就过不去,因为他是byName的

看来还是不用这个validate 插件了
sl514 2008-05-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sl514 的回复:]
修正
你说的name="user.code" 应该是struts2的方法 你可以这样做remote: "xxx.jsp?code"+$("#userCode").val(),

至于 <ss:textfield id="userCode" name="user.code"/>,这样写的话,jquery的validate验证不起作用 你那就得重新看validate的插件了

下面是我的代码
$("#username").formValidator({onshow:"请输入用户名",onfocus:"用户名至少4个字符,最多10个字符",oncorrect:"该用户名可以注册"}).InputValidat…
[/Quote]

哦 var a = data.toJSONString();
alert(a); 是我在测试json插件做的代码 可以去掉 呵呵 测试的
sl514 2008-05-04
  • 打赏
  • 举报
回复
修正
你说的name="user.code" 应该是struts2的方法 你可以这样做remote: "xxx.jsp?code"+$("#userCode").val(),

至于<ss:textfield id="userCode" name="user.code"/>,这样写的话,jquery的validate验证不起作用 你那就得重新看validate的插件了

下面是我的代码
$("#username").formValidator({onshow:"请输入用户名",onfocus:"用户名至少4个字符,最多10个字符",oncorrect:"该用户名可以注册"}).InputValidator({min:4,max:10,onerror:"你输入的用户名非法,请确认"}).RegexValidator({regexp:"username",datatype:"enum",onerror:"用户名格式不正确"}).AjaxValidator({
type : "post",
url : $("#hid").val()+"?user.name="+$("#username").val(),
datatype : "json",
success : function(data){
var a = data.toJSONString();
alert(a);
if( data.indexOf("true")!=-1 )
{
return true;
}
else
{
return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "该用户名不可用,请更换用户名",
onwait : "正在对用户名进行合法性校验,请稍候..."
}).DefaultPassed();

sl514 2008-05-04
  • 打赏
  • 举报
回复
这个问题我也有遇到过 但是我要说的 是 首先jQuery的取值 基本上是用id 也就是$("#id") 基本上不用name 除非是tagName

其次我用过 id="user.name" 好像有个jQuery高手告诉我 $("#user\\.name") 是可以娶到的

你说的name="user.code" 应该是struts2的方法 你可以这样做remote: "xxx.jsp?"+$("#test").val(),

如果上面的方法不行 那你就得考虑在你的action里 设个新字段来接受他 如果还不行 就加我QQ 624767717 我是玩jQuery和Struts2的
Landor2004 2008-05-04
  • 打赏
  • 举报
回复
不会吧,没人用过吗??

52,792

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧