jquery validate一个表单(单选按钮上什么操作)分别验证

走在四季 2015-09-08 02:40:10
一个表单,想要分别验证。
<tr>
<td width="15%" height="30" align="right">新用户:</td>
<td width="60%"><input type="radio" name="isnewuser" value="0" checked="checked"/>否<input type="radio" name="isnewuser" value="1" style="margin-left:30px;"/>是</td>
</tr>


如果不是新用户,只验证用户名是否存在。
如果是新用户,下面隐藏的用户注册信息显示,让用户注册并验证。

问了下,大概意思是说在radio上有个什么操作就行。。

求示例答案,谢谢。
...全文
296 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
走在四季 2015-09-08
  • 打赏
  • 举报
回复
引用 5 楼 showbo 的回复:
你的代码要放到表单下面,如果你要使用validate组件,你需要使用自定义规则,然后增加上f.isnewuser[0].checked的判断,不能按照我原来的方法,要不只注册了旧用户的验证,如果注册用户点击新注册按钮就无效了 没怎么用这个插件,你看下能不能动态添加移除规则,如果可以你判断点击了新或者旧,移除对应的规则

<td><input type="radio" id="isnewuser" name="isnewuser" value="0" checked="checked"/>否<input type="radio" id="isnewuser1" name="isnewuser" value="1" style="margin-left:30px;"/>是</td>

oldloginName:{
				required:"#isnewuser:checked",
				remote: "${ctx}/user!checkuser.action"
			},
			newloginName:{
				required:"#isnewuser1:checked",
				loginid:true,
				minlength:6,
				maxlength:20
			},
亲,找到解决方法了,是这样弄两个ID,然后也是看那个选中,然后分别验证的。谢谢亲。
Go 旅城通票 2015-09-08
  • 打赏
  • 举报
回复
你的代码要放到表单下面,如果你要使用validate组件,你需要使用自定义规则,然后增加上f.isnewuser[0].checked的判断,不能按照我原来的方法,要不只注册了旧用户的验证,如果注册用户点击新注册按钮就无效了 没怎么用这个插件,你看下能不能动态添加移除规则,如果可以你判断点击了新或者旧,移除对应的规则
走在四季 2015-09-08
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
var f=document['你的表单id'];
if(f.isnewuser[0].checked){//旧用户
//....验证代码
}
else{
//...验证代码
}




亲,不好意思,刚很丢人的是弄错了页面。这回是对的页面。。
走在四季 2015-09-08
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
var f=document['你的表单id'];
if(f.isnewuser[0].checked){//旧用户
//....验证代码
}
else{
//...验证代码
}

var f=document['inputForm'];
	alert(f.isnewuser[0].checked);
	if(f.isnewuser[0].checked){
		alert("111");
		$("#inputForm").validate({
			rules:{
				oldloginName:{
					required:true,
					remote: "${ctx}/user!checkuser.action"
				}
			},
			messages:{
				oldloginName:{
					required:"必填",
					remote: "用户名已存在"
				}
			},
			errorPlacement: function(error, element) {
			    error.appendTo(element.parent());
			}
		});
	}else{
		alert("222");
		$.validator.addMethod("loginid", function(value,element) {
			var loginid = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]+$/;
			return this.optional(element) || (loginid.test(value));
		}, '数字、字母和下划线');
		$("#inputForm").validate({
			rules:{
				newloginName:{
					required:true,
					loginid:true,
					minlength:6,
					maxlength:20
				},
				newplainPassword:{
					required:true,
					minlength:6,
					maxlength:20
				},
				newname:{
					required:true
				},
				newemail: {
		            required: true,
		            email: true,
		            maxlength:60,
					remote: "${ctx}/user!checkemail.action"
		        },
			},
			messages:{
				newloginName: {
					required:"必填",
		    		minlength:"不能少于6个字符!",
					maxlength:"不能超过20个字符!",
					remote: "用户名已存在"
				},
				newplainPassword:{
					required:"必填",
				},
				newemail: {
		            email: "格式不正确!",
					remote: "邮箱已存在"
		        }
			},
			errorPlacement: function(error, element) {
			    error.appendTo(element.parent());
			}
		});
	}
亲,麻烦再帮我看下,alert一个都没有弹出。页面也没有发现JS报错。
走在四季 2015-09-08
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
var f=document['你的表单id'];
if(f.isnewuser[0].checked){//旧用户
//....验证代码
}
else{
//...验证代码
}
谢谢,我去试下,弄明白了就结贴给分。 亲,祝你天天好心情,事事顺心,万事如意。
Go 旅城通票 2015-09-08
  • 打赏
  • 举报
回复
var f=document['你的表单id'];
if(f.isnewuser[0].checked){//旧用户
//....验证代码
}
else{
//...验证代码
}

87,845

社区成员

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

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