[英]jQuery: check username availability
我在Web應用程序上注冊時使用jQuery檢查用戶名是否可用。 由於某種原因,而不是將用戶數據保留在數據庫中,我將注冊用戶的ID和密碼存儲在平面文件account.txt中。 格式如下:
喬:frt25t5546g
約翰:sdfsdgg
更新資料
多虧了這里的人的幫助,我終於有了一個線索,我遵循了http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html上的方法。
這是我的registration.html的一部分
$(document).ready(function()
{
$("#uname").blur(function(){
$.post("usernameCheck.php",{user_name:$(this).val()},function(data)
{
if(data=="no"){
$(this).html("This username already exists");
}
else{
$(this).html("Username is available!");
}
});
});
}
<body>
<form name="form" method="post" action="" />
<table>
<tr><td>Username: </td>
<td><input type="text" name="uname" id="uname" /></td></tr>
</table>
<input type="submit" name="submit" value="Register" />
</form>
</body>
我對jQuery很陌生,對ajax有點...謝謝!
不要在客戶端這樣做。 您的密碼,即使是加密的,也絕對不能離開服務器。 創建一個服務器端腳本,該腳本將接受用戶名並返回布爾值,然后通過AJAX進行調用。
另外,要回答您的直接問題,請查找JavaScript方法String.split
和String.match
。
建議不要使用此代碼,但例如,您可以這樣做
$(function(){
$parsed={};
$.ajax({
url: 'userpass.txt',
complete: function(data, textStatus, jqXHR) {
// get all the data and separate it by rows
$.each(data.responseText.split("\n"), function(i,v){
// split the rows by colon sign
$parsed[v.split(":")[0]] = v.split(":")[1];
});
}
});
// when the user is typing the username
$('input#username').keyup(function(){
v = $(this).val();
// check if the username exists
if ($parsed[v]) {
alert("user exists");
}
});
});
我希望這個例子可以說明實際執行此操作有多不安全。
HTML:
<form id="secureForm">
<input type="text" id="username"/>
<input type="password" id="password"/>
<input type="submit" id="submit" value="Submit"/>
<div id="results"></div>
</form>
CSS:
#results {
margin-top: 10px;
}
Javascript:
$(document).ready(function() {
$("#username").keyup(function(e) {
totallySecureWayToCheckIfUserExists();
});
});
function totallySecureWayToCheckIfUserExists() {
$.post("/echo/html/", {
html: "joe:frt25t5546g\njohn:sdfsdgg\nthis:isstupid"
}, function(data) {
var users = data.split("\n");
$("#results").html("");
$.each(users, function(index, row) {
var username = row.split(":")[0];
var password = row.split(":")[1];
if(username == $("#username").val()) {
$("#results").html("").append("<p>"+"Result found! Username '" + username +"' with the password '"+password+"' exists, you cannot use this username sorry!</p>");
return false;
}
else {
$("#results").append("<p>"+"Doesn't match the username: " + username +" (password="+password+")</p>");
}
});
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.