2011-12-10 51 views
3

我想使用用戶名採取或不使用jquery驗證插件。但劑量似乎工作。我想知道我哪裏錯了,所有其他驗證工作正常。除此之外。用戶名採取與jquery驗證插件檢查

JQuery驗證插件頁面:http://bassistance.de/jquery-plugins/jquery-plugin-validation/

的JavaScript

<script type="text/javascript"> 
     $(document).ready(function(){ 
     $("#register").validate({ 
      debug: false, 
      rules: { 
       username: { 
       required: true, 
       minlength: 2, 
       remote: "users.php" 
      }, 
       email: { 
        required: true, 
        email: true 
       }, 
      password: { 
       required: true, 
       minlength: 5 
      }, 
      confirm_password: { 
       required: true, 
       minlength: 5, 
       equalTo: "#password" 
      } 
      }, 
      messages: { 
       username: { 
       required: "Please enter a username", 
       minlength: "Your username must consist of at least 2 characters", 
       remote: jQuery.format("{0} is already in use") 
      }, 
       email: "A valid email will help us get in touch with you.", 
      }, 
      password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 5 characters long" 
      }, 
      confirm_password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 5 characters long", 
       equalTo: "Please enter the same password as above" 
      }, 
      submitHandler: function(form) { 
       // do other stuff for a valid form 
       $.post('adduser.php', $("#register").serialize(), function(data) { 
       $("#register").fadeOut('fast', function(){ 
        $('#results').html(data); 
       }); 
       }); 
      } 
     }); 
    }); 
</script> 

users.php

<?php 
$request = $_REQUEST['username']; 

//usleep(150000); 
$query = mysql_query("SELECT * FROM users WHERE username ='$username'"); 
$result = mysql_num_rows($query); 
if ($result == 0){ 
$valid = 'true';} 
else{ 
$valid = 'false'; 
} 
echo $valid; 
?> 

register.php

<form name="register" id="register" method="post" action=""> 
<section> 
<label for="username">Username:</label> 
<div> 
<input type="text" tabindex="1" class="input" id="username" name="username" value=""/> 
</div> 
</section> 
<!--#--> 
<section> 
<label for="email">email</label> 
<div> 
<input type="text" tabindex="2" class="input" id="email" name="email" value=""/> 
</div> 
</section> 
<!--#--> 
<section> 
<label for="password">Password</label> 
<div> 
<input type="password" tabindex="3" class="input" id="password" name="password" value=""/> 
</div> 
</section> 
<!--#--> 
<section> 
<label for="confirm_password">Confirm Password</label> 
<div> 
<input type="password" tabindex="4" class="input" id="confirm_password" name="confirm_password" value=""/> 
</div> 
</section> 
<!--#--> 
<br/> 
<input type="submit" tabindex="5" id="submit" value="REGISTER" /> 
</form> 
<div id="results"> </div> 

感謝進階ANCE。

回答

0

要回答OP,您可能需要使用$ _GET。此外,從PHP 5.5.0起,mysql_num_rows已被棄用,因此您可能需要更新它。你可以檢查查詢是否返回false。

<?php 
$request = $_GET['username']; 

//usleep(150000); 
$query = mysql_query("SELECT * FROM users WHERE username ='$username'"); 
if ($query == false){ 
$valid = 'true';} // username available 
else{ 
$valid = 'false'; // username unavailable 
} 
echo $valid; 
?> 
2

我有相同的,但我使用他們的驗證和this來檢查用戶名是否存在。它效果很好。

+0

嗨tnx的信息,但這並不能阻止用戶註冊,即使用戶名已存在的權利?你介意和我一起剪切你的代碼嗎? – maxlk

+0

我所做的就是隱藏保存按鈕,除非用戶名不存在,否則顯示按鈕。 – Andres

1

首先,你必須是過早關閉郵件選項,導致剩餘的消息的額外}被忽略。在confirm_password消息塊之後,您應該移動此}。其次,您應該致電jQuery.validator.format而不是jQuery.format。我相信這就是爲什麼這個特定的驗證規則不起作用。糾正這些,看看它是否適合你。

+0

如果我刪除'}'代碼將停止工作。其他所有驗證工作都很好。用戶名檢查除外。 – maxlk

+0

@maxlk我沒有說刪除它,我說在confirm_password塊之後移動它。你不能看到它過早地終止你的消息選項嗎?無論如何,我認爲這是你錯誤的格式函數調用導致你描述的問題。 – tvanfosson

+0

試過這種方法。但沒有什麼不同。所有其他人正在驗證,除了用戶名檢查。 – maxlk

0

試試這個......

JS

$("#register").validate({ 
     debug: false, 
     rules: { 
      username: { 
      required: true, 
      minlength: 6, 
      remote: { 
       url: "Your PHP", 
       type: "post", 
       data: { 
       funct: 'user_check', 
       username: function() { 
        return $("#username").val(); 
       } 
       } 
      } 
     }, 
     messages: { 
      username: { 
      required: "Please enter a username", 
      minlength: "Your username must consist of at least 2 characters", 
      remote: jQuery.format("{0} is already in use") 
     } 
    }); 
}); 

PHP

<?php 
    function user_check(){ 
    $username = $_POST['username']; 
    $query = mysql_query("SELECT * FROM users WHERE username ='$username'"); 
    if ($query == false){ 
     $valid = 'true';} // username available 
    else{ 
     $valid = 'false'; // username unavailable 
    } 
     echo $valid; 
    } 
?> 

可能有幫助..