欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ajax 检测用户名是否被占用

程序员文章站 2022-06-17 13:36:07
复制代码 代码如下://检测用户名是否被占用 function geto(){ var ajax=false; try { ajax = new activexobject...

复制代码 代码如下:

//检测用户名是否被占用
function geto(){
var ajax=false;
try {
ajax = new activexobject("msxml2.xmlhttp");
} catch (e) {
try {
ajax = new activexobject("microsoft.xmlhttp");
} catch (e) {
ajax = false;
}
}
if (!ajax && typeof xmlhttprequest!='undefined') {
ajax = new xmlhttprequest();
}
return ajax;
}
function getresult(serverpage) {
var ajax = geto();
var sep = (-1 < serverpage.indexof("?")) ? "&" : "?"
var newurl = serverpage + sep + "__=" + encodeuricomponent((new date()).tostring());

ajax.open("get", newurl, true);
ajax.onreadystatechange = function() {
if (ajax.readystate == 4 && ajax.status == 200) {
if(ajax.responsetext==1){
showresult(1,"您的用户名可以正常注册")
ajaxresult=true;
}else{
showresult(0,"您的用户名被占用了")
ajaxresult=false;
}
}
}
ajax.send(null);

}
function chkusername(){
var sflag;//标记;
var serr;//错误信息;
sflag=true;

var re=/^\w{2,20}$/;
if(!re.test(document.form1.username.value)){
sflag=false;
serr="用户名只能输入字母、数字或下划线";
}

if (document.form1.username.value.length>20||document.form1.username.value.length<2){
sflag=false;
serr="用户名长度必须在2~20个字符之间";
}
if (document.form1.username.value=="" ){
serr="用户名不能为空";
sflag=false;
}
//ajax检测返回结果
if (!sflag) {
showresult(0,serr)
}else{
getresult("../membercheck.aspx?t=1&name=" + document.form1.username.value);
}

//document.getelementbyid("spname").innerhtml=ss;
}
function showresult(tag,str){
var obj = document.getelementbyid("spname");
if(tag==0){
obj.style.border="1px solid red";
obj.style.color="red";

}else{
obj.style.border="1px solid green";
obj.style.color="green";
}
obj.innerhtml=str;
obj.style.padding="1px";
}
//取ajax返回的用户问题值
function getquestiondata(spage){
var ajax = geto();
var sep = (-1 < spage.indexof("?")) ? "&" : "?"
var newurl = spage + sep + "__=" + encodeuricomponent((new date()).tostring());

ajax.open("get", newurl, true);
ajax.onreadystatechange = function() {
if (ajax.readystate == 4 && ajax.status == 200) {
if(ajax.responsetext==""){
document.getelementbyid("getstate").innerhtml="用户不存在!"
}
else{
document.g.question.value=ajax.responsetext;
document.g.question.readonly=true;
document.getelementbyid("getpwdstep1").style.display="none";
document.getelementbyid("getpwdstep2").style.display="block";
document.getelementbyid("getstate").innerhtml="";

}
}
}
ajax.send(null);
}
function getuserq(){
//改变报错span的显示样式
document.getelementbyid("getstate").style.padding="1px 4px";
if (document.g.username.value==""){
document.getelementbyid("getstate").innerhtml="用户名不能为空!"
}else{
document.getelementbyid("getstate").style.background="red";
document.getelementbyid("getstate").style.color="white";
document.getelementbyid("getstate").innerhtml="正在加载数据...";
//设置ajax
getquestiondata("../membercheck.aspx?t=2&gname=" + document.g.username.value);
}
}

function login(n,p,t)
{
if(t==false){
document.getelementbyid("plogin").style.display="none";
document.getelementbyid("logininfo").innerhtml="正在帮您登录,请稍等...";
}
var request;
if(undefined == request){
request = new request();
}

if(undefined == request._get){
//不支持ajax获取数据;
if(t==false){
document.getelementbyid("plogin").style.display="";
document.getelementbyid("logininfo").innerhtml="不能获取数据";
}
}
else{
username=n;
toprev=t;
request.getnocache(loginpath + "logincheck.aspx" + "?n=" + n + "&p=" + p,logincheck);
}
}
function logincheck(result)
{
if (result.readystate!=readystate.complete)
return;
//成功获取;
if (result.status==httpstatus.ok && result.responsetext != "")
{
//执行返回的语句;
//alert(result.responsetext);
if (result.responsetext=='1')
{
if(toprev ==false){
document.getelementbyid("logininfo").innerhtml='<ul><li>欢迎' + username + '</li><li><a href="' + loginpath + 'member/">管理</a></li><li><a href="' + loginpath + 'logout.aspx">退出</a></li></ul>';
}
else{
if(urlprev != ''){
document.location.href=urlprev;
}
else{
document.location.href=loginpath;
}
}
return;
}
else
{
if(document.location.href !="http://" + document.location.hostname + loginpath + "member/login.aspx")
document.location.href=loginpath + "member/login.aspx";
}
}
else
{
//失败;
document.location.href=loginpath + "member/login.aspx";
return;
}
}
function checkkey(caputurekey,n,p,t){
var key = window.event.keycode;
if(key == caputurekey){
login(n,p,t);
}
}
function setlogininfo(blogin){
if(blogin){
document.getelementbyid("logininfo").innerhtml='<ul><li>欢迎' + username + '</li><li><a href="' + loginpath + 'member/">管理</a></li><li><a href="' + loginpath + 'logout.aspx">退出</a></li></ul>';
}
else{
}
}
function gotourl(s){
window.location.href=loginpath + 'member/reguser.html';
}