我正在尝试添加远程方法来检查jquery验证的登录可用性。我读过其他问题,但它没有帮助我...... 它不起作用。我不知道我哪里错了
$('#contact-form').validate({
rules: {
login: {
required: true,
remote: {
url: "user_availability.php",
type: "post",
data:
{
login: function()
{
return $('#contact-form :input[name="login"]').val();
}
}
}
}
},
messages:{
login:{
required: "Please enter your login.",
remote: jQuery.validator.format("{0} is already taken.")
}
}
});
文件user_availability.php:
<?php
$existing_users=array('admin','mike','jason');
$user_name=$_POST['user_name'];
if (in_array($user_name, $existing_users))
{
echo "false"; //already registered
}
else
{
echo "true"; //user name is available
}
?>
答案 0 :(得分:4)
这是因为在jQuery验证中,您将字段名称视为“登录”,而在PHP文件中,您使用user_name ..两者都不同。
请使用以下代码告诉我......
<script>
$(function(){
$('#contact-form').validate({
rules: {
user_name: {
required: true,
remote: {
url: "user_availability.php",
type: "post",
data:
{
login: function()
{
return $('#contact-form :input[name="user_name"]').val();
}
}
}
}
},
messages:{
user_name:{
required: "Please enter your login.",
remote: jQuery.validator.format("{0} is already taken.")
}
}
});
});
</script>