login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
<style type="text/css">
* {margin:0}
body {text-align:center;min-width:760px}
div {padding:3px 3px 3px 3px}
#main {width:720px;margin: 0 auto;text-align:left;margin-top: 30px}
#main div span {width: 50px}
</style>
<script type="text/javascript"> /***
* 设置Cookie
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} nDays Cookie过期天数
*/
function SetCookie(cookieName, cookieValue, nDays) {
/* 当前日期 */
var today = new Date();
/* Cookie过期时间 */
var expire = new Date();
/* 如果未设置nDays参数或者nDays为0,取默认值1 */
if (nDays == null || nDays==0) nDays=1;
/* 计算Cookie过期时间 */
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
/* 设置Cookie值 */
document.cookie = cookieName + "=" + escape(cookieValue) +
";expires="+expire.toGMTString();
//location.href="1.html";
}
function login() {
// 用户名
var username = $("user").value;
// 密码
var password = $("pass").value;
// 是否7天内无需登录
var save = $("save").checked;
if(username!=""&&password!=""){
// 在Cookie中保存用户名
if (save) SetCookie("username", username, 7);
else SetCookie("username", username, 1);
alert("登录成功!");
}else {
alert("用户名或密码错误!");
}
//// 合法的用户名/密码是test/password
// if (username == "test" && password == "password") {
// // 在Cookie中保存用户名
// if (save) SetCookie("username", username, 7);
// else SetCookie("username", username, 1);
//
// // 跳转到gallary.html页面
// document.location = "gallary.html";
// } else {
// alert("用户名或密码错误!");
// }
}
function $(id) {
return document.getElementById(id);
}
function register(){
document.location = "register.html";
}
</script>
</head><body onload="init()">
<p> </p>
<div id="main">
<div>
<p>
<label></label>
</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><span>用户名:</span>
<input type="text" id="user" />
</p>
</div>
<div>
<span>密码:</span><input type="password" id="pass" /></div>
<div>
<input type="checkbox" id="save" />
14天内无需登录
<input type="button" onclick="login()" value="登录" />
<input type="button" onclick="register()" value="注册" />
</div>
</div>
</body>
</html>register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>注册页面</title>
<script language="javascript">
function check(){
if(document.form1.username.value.length==0){
alert("请输入用户名");
checkUser();
return false;
}
if(document.form1.pass.value.length==0){
alert("请输入密码!");
return false;
}
if(document.form1.pass.value.length<6||document.form1.pass.value.length>13){
alert("密码位数大于6小于13!");
return false;
}
if(document.form1.password.value.length==0){
alert("请再次输入密码!");
return false;
}
if(document.form1.pass.value!=document.form1.password.value){
alert("两次次输入密码不一致!");
return false;
}
if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(document.form1.email.value)){
alert("邮箱格式不正确!");
return false;
}
//alert("注册成功!");
return true;
}
function checkUser(username){
var obj=document.all.form1;
var j=0;
for(var i=0;i<obj.username.length;i++){
if(obj.username[i].value==obj.username.value){
j++;
}else{}
}
if(j>0){
alert("此用户已存在!");
return false;
}else{
alert("可以使用此用户名!");
return ;
}
}
function SetCookie(cookieName, cookieValue, nDays) {
/* 当前日期 */
var today = new Date();
/* Cookie过期时间 */
var expire = new Date();
/* 如果未设置nDays参数或者nDays为0,取默认值1 */
if (nDays == null || nDays==0) nDays=1;
/* 计算Cookie过期时间 */
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
/* 设置Cookie值 */
document.cookie = cookieName + "=" + escape(cookieValue) +
";expires="+expire.toGMTString();
//location.href="1.html";
}
function ReadCookie(cookieName) {
// Cookie字符串
var theCookie = "" + document.cookie;
// 找到cookieName的位置
var ind = theCookie.indexOf(cookieName);
// 如果未找到cookieName,返回空字符串
if (ind == -1 || cookieName == "") return "";
// 确定分号的位置
var ind1 = theCookie.indexOf(';', ind);
if (ind1 == -1) ind1 = theCookie.length;
// 读取Cookie值
return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}function $(id) {
return document.getElementById(id);
}</script>
</head><body>
<form id="form1" name="form1" onsubmit="return check()" method="post" action="">
用户名:
<label>
<input name="username" type="text" id="username" />
<input type="submit" name="button" id="button" value="检查用户名" onclick="checkUser(document.all.username.value)" />
</label>
<p>密 码:
<label>
<input name="pass" type="password" id="pass" />
</label>
</p>
<p>确认密码:
<label>
<input name="password" type="password" id="password" />
</label>
</p>
<p>Email:
<label>
<input name="email" type="text" id="email" />
</label>
</p>
<p>性 别:
<label>
<input name="radiobutton" type="radio" value="radiobutton" checked="checked" />
</label>
<label> </label>
男
<label>
<input type="radio" name="radiobutton" value="radiobutton" />
</label>
女</p>
<p>出生年月:
<label>
<select name="year" size="1" id="year">
<option selected="selected">1985</option>
<option>1986</option>
<option>1987</option>
<option>1988</option>
<option>1989</option>
<option>1990</option>
<option>1991</option>
<option>1992</option>
<option>1993</option>
<option>1994</option>
<option>1995</option>
<option>1996</option>
<option>1997</option>
<option>1998</option>
<option>1999</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
</select>
</label>
<label>
<select name="month" size="1" id="month" >
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</label>
<label>
<select name="day" size="1" id="day">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交"/>
</label>
<label>
<input name="cancel" type="reset" id="cancel" value="取消" />
</label>
</p>
</form>
</body>
</html>如何把注册信息保存在cookie中,并可以用注册的用户名和密码登录,未注册的用户名不能登录?请高手指教一下!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
<style type="text/css">
* {margin:0}
body {text-align:center;min-width:760px}
div {padding:3px 3px 3px 3px}
#main {width:720px;margin: 0 auto;text-align:left;margin-top: 30px}
#main div span {width: 50px}
</style>
<script type="text/javascript"> /***
* 设置Cookie
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} nDays Cookie过期天数
*/
function SetCookie(cookieName, cookieValue, nDays) {
/* 当前日期 */
var today = new Date();
/* Cookie过期时间 */
var expire = new Date();
/* 如果未设置nDays参数或者nDays为0,取默认值1 */
if (nDays == null || nDays==0) nDays=1;
/* 计算Cookie过期时间 */
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
/* 设置Cookie值 */
document.cookie = cookieName + "=" + escape(cookieValue) +
";expires="+expire.toGMTString();
//location.href="1.html";
}
function login() {
// 用户名
var username = $("user").value;
// 密码
var password = $("pass").value;
// 是否7天内无需登录
var save = $("save").checked;
if(username!=""&&password!=""){
// 在Cookie中保存用户名
if (save) SetCookie("username", username, 7);
else SetCookie("username", username, 1);
alert("登录成功!");
}else {
alert("用户名或密码错误!");
}
//// 合法的用户名/密码是test/password
// if (username == "test" && password == "password") {
// // 在Cookie中保存用户名
// if (save) SetCookie("username", username, 7);
// else SetCookie("username", username, 1);
//
// // 跳转到gallary.html页面
// document.location = "gallary.html";
// } else {
// alert("用户名或密码错误!");
// }
}
function $(id) {
return document.getElementById(id);
}
function register(){
document.location = "register.html";
}
</script>
</head><body onload="init()">
<p> </p>
<div id="main">
<div>
<p>
<label></label>
</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><span>用户名:</span>
<input type="text" id="user" />
</p>
</div>
<div>
<span>密码:</span><input type="password" id="pass" /></div>
<div>
<input type="checkbox" id="save" />
14天内无需登录
<input type="button" onclick="login()" value="登录" />
<input type="button" onclick="register()" value="注册" />
</div>
</div>
</body>
</html>register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>注册页面</title>
<script language="javascript">
function check(){
if(document.form1.username.value.length==0){
alert("请输入用户名");
checkUser();
return false;
}
if(document.form1.pass.value.length==0){
alert("请输入密码!");
return false;
}
if(document.form1.pass.value.length<6||document.form1.pass.value.length>13){
alert("密码位数大于6小于13!");
return false;
}
if(document.form1.password.value.length==0){
alert("请再次输入密码!");
return false;
}
if(document.form1.pass.value!=document.form1.password.value){
alert("两次次输入密码不一致!");
return false;
}
if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(document.form1.email.value)){
alert("邮箱格式不正确!");
return false;
}
//alert("注册成功!");
return true;
}
function checkUser(username){
var obj=document.all.form1;
var j=0;
for(var i=0;i<obj.username.length;i++){
if(obj.username[i].value==obj.username.value){
j++;
}else{}
}
if(j>0){
alert("此用户已存在!");
return false;
}else{
alert("可以使用此用户名!");
return ;
}
}
function SetCookie(cookieName, cookieValue, nDays) {
/* 当前日期 */
var today = new Date();
/* Cookie过期时间 */
var expire = new Date();
/* 如果未设置nDays参数或者nDays为0,取默认值1 */
if (nDays == null || nDays==0) nDays=1;
/* 计算Cookie过期时间 */
expire.setTime(today.getTime() + 3600000 * 24 * nDays);
/* 设置Cookie值 */
document.cookie = cookieName + "=" + escape(cookieValue) +
";expires="+expire.toGMTString();
//location.href="1.html";
}
function ReadCookie(cookieName) {
// Cookie字符串
var theCookie = "" + document.cookie;
// 找到cookieName的位置
var ind = theCookie.indexOf(cookieName);
// 如果未找到cookieName,返回空字符串
if (ind == -1 || cookieName == "") return "";
// 确定分号的位置
var ind1 = theCookie.indexOf(';', ind);
if (ind1 == -1) ind1 = theCookie.length;
// 读取Cookie值
return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}function $(id) {
return document.getElementById(id);
}</script>
</head><body>
<form id="form1" name="form1" onsubmit="return check()" method="post" action="">
用户名:
<label>
<input name="username" type="text" id="username" />
<input type="submit" name="button" id="button" value="检查用户名" onclick="checkUser(document.all.username.value)" />
</label>
<p>密 码:
<label>
<input name="pass" type="password" id="pass" />
</label>
</p>
<p>确认密码:
<label>
<input name="password" type="password" id="password" />
</label>
</p>
<p>Email:
<label>
<input name="email" type="text" id="email" />
</label>
</p>
<p>性 别:
<label>
<input name="radiobutton" type="radio" value="radiobutton" checked="checked" />
</label>
<label> </label>
男
<label>
<input type="radio" name="radiobutton" value="radiobutton" />
</label>
女</p>
<p>出生年月:
<label>
<select name="year" size="1" id="year">
<option selected="selected">1985</option>
<option>1986</option>
<option>1987</option>
<option>1988</option>
<option>1989</option>
<option>1990</option>
<option>1991</option>
<option>1992</option>
<option>1993</option>
<option>1994</option>
<option>1995</option>
<option>1996</option>
<option>1997</option>
<option>1998</option>
<option>1999</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
</select>
</label>
<label>
<select name="month" size="1" id="month" >
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</label>
<label>
<select name="day" size="1" id="day">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交"/>
</label>
<label>
<input name="cancel" type="reset" id="cancel" value="取消" />
</label>
</p>
</form>
</body>
</html>如何把注册信息保存在cookie中,并可以用注册的用户名和密码登录,未注册的用户名不能登录?请高手指教一下!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货