js部分
<script>
$().ready(function() {
var name = document.getElementById("name").value;//根据id获取用户名
$("#myForm").validate({ //表单属性id = myForm
submitHandler:function(form){
form.submit(); //表单提交
},
rules: {
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 6,
maxlength: 20
},
password_again: {
required: true,
equalTo:"#password" //和id = password 的内容值相同
},
name: {
required: true,
minlength: 6,
maxlength: 20,
remote:{
url: "{:U('home/self/name_check')}", //后台处理程序 thinkphp写法 反正写链接就对了
type: "post", //数据发送方式
dataType: "json", //接受数据格式
data: { //要传递的数据
'name':name
}
}
}
},
messages: {
email:{
required:"请填写账号!",
email:"邮箱格式不正确!"
},
password:{
required:"请填写密码!",
minlength:"最短不得少于6位!",
maxlength:"密码长度不得超过20"
},
password_again:{
required:"请再次输入密码!",
equalTo:"两次密码不一致!"
},
name:{
remote:"该用户名称已存在!",
required:"请填写账号!",
minlength:"最短不得少于6位!",
maxlength:"账号长度不得超过20位"
}
}
});
});
php 代码部分:
public function name_check(){
if(IS_POST){
$name = I('post.name');
$user_model = M("User");
$condition = array(
'name'=>$name
);
$user_count = $user_model->where($condition)->count();;
if($user_count){
echo 'false';
//这里超级邪门,必须小写单引号否则无效,查了很久不知道为什么
}else{
echo 'true';
}
}
}