validator.js remote 返回什么类型
2016-05-15 · 百度知道合伙人官方认证企业
validator.js remote 返回类型是根据表单比较的结果一般是返回字符串类型。
有时候不仅仅对表单所录入的信息进行验证还需要将录入的值与数据库进行比较,这时就需要借助remote方法来实现。以下介绍 jquery.validate.js的后台验证的remote方法,准备工作,前台页面:
<script src="test/jquery.js" type="text/javascript"></script>
<script src="test/jquery.validate.js" type="text/javascript"></script>
<script src="test/jquery.validate.message_cn.js" type="text/javascript"></script>
</head>
<body>
<form name="test" id="testform" method="get" action="get.php">
<fieldset>
<legend title="用户注册(User Register)">用户注册(User Login)</legend>
<p>
<label for="name">用户名:</label>
<input id="name" name="name" type="text" />
</p>
<p>
<label for="mail">邮箱:</label>
<input id="mail" name="mail" type="password" />
</p>
<p>
<label for="password">密码:</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label for="repassword">重复密码:</label>
<input id="repassword" name="repassword" type="password" />
</p>
<p>
<label for="hash">邀请码:</label>
<input id="hash" name="hash" type="text" />
</p>
<p>
<label for="sel">选择:</label>
<select id="sel" name="sel">
<option value="">请选择</option>
<option value="1">选择1</option>
<option value="2">选择2</option>
<option value="3">选择3</option>
<option value="4">选择4</option>
</select>
</p>
<p>
<label for="type">用户类型:</label>
<span><input name="type" type="radio" value="1" />类型1</span>
<span><input name="type" type="radio" value="2" />类型2</span>
<span><input name="type" type="radio" value="3" />类型3</span>
</p>
<p>
<label for="submit"> </label>
<input class="submit" type="submit" value="注册"/>
</p>
</fieldset>
</form>
要实现的效果:
验证代码:
<script type="text/javascript">
$(function(){
$("#testform").validate({
rules : {
name : {
required : true
},
password: {
required: true,
minlength: 5
},
repassword: {
required: true,
minlength: 5,
equalTo: "#password"
},
hash: {
required: true,
remote: 'checkhash.php'
},
sel: {
remote: 'checksel.php'
},
type: {
remote:{
url: "changeusertype.php",
type: "get",
dataType: 'json',
data: {
'type': function(){return $('input[name="type"]:checked').val();}
}
}
}
},
messages : {
name : {
required : '必填'
},
password: {
required: '必填',
minlength: '最少5个字符'
},
repassword: {
required: '必填',
minlength: '最少5个字符',
equalTo: '两次输入的密码不一样'
},
hash: {
required: '必填',
remote: '邀请码不正确'
},
sel: {
remote: '选择不正确'
},
type: {
remote: '类型不可更改'
}
},
focusInvalid: true,
/*指定错误信息位置*/
errorPlacement: function (error, element) {
error.appendTo(element.closest("p"));
},
//设置错误信息存放标签
errorElement: "em",
submitHandler: function(form) {
}
});
})
</script>
验证结果如下: