AJAX使用前端校验用户名是否已被使用------AJAX

134 篇文章 0 订阅
45 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en" id="myHtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HtmlAll</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function()
    {
        document.getElementById("username").onblur = function()
        {
            var value = document.getElementById("username").value;
            var ajax = new XMLHttpRequest();
            ajax.onreadystatechange = function()
            {
                if(ajax.readyState === 4)
                {
                    if(ajax.status === 200)
                    {
                        if(ajax.responseText === "该用户名已被使用")
                        {
                            document.getElementById("usernameError").innerHTML = ajax.responseText;
                        }
                        else
                        {
                            document.getElementById("usernameError").innerHTML = "可用该用户名";
                        }
                    }
                    else
                    {
                        alert(ajax.status);
                    }
                }
            }
            ajax.open("POST","/ajax/ajaxRequestTest",true);
            ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            ajax.send("username=" + value);
        }
    }
</script>
<table align="center">
    <form>
        <tr align="center">
            <th>用户名</th>
            <th><input type="text" name="username" id="username"/></th>
            <th id="usernameError" style="color: red;"></th>
        </tr>
        <tr align="center">
            <th>邮箱</th>
            <th><input type="email" name="username" id="email"/></th>
        </tr>
        <tr align="center">
            <th>密码</th>
            <th><input type="password" name="username" id="password1"/></th>
        </tr>
        <tr align="center">
            <th>确认密码</th>
            <th><input type="password" name="username" id="password2"/></th>
        </tr>
        <tr>
            <th>登录或重置</th>
            <th>
                <input type="submit" name="submit" id="登录"/>
                <input type="reset" name="reset" id="重置"/>
            </th>
        </tr>
    </form>
</table>
<div id="myDiv"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" id="myHtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HtmlAll</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function()
    {
        document.getElementById("username").onblur = function()
        {
            var value = document.getElementById("username").value;
            var ajax = new XMLHttpRequest();
            ajax.onreadystatechange = function()
            {
                if(ajax.readyState === 4)
                {
                    if(ajax.status === 200)
                    {
                        if(ajax.responseText === "该用户名已被使用")
                        {
                            document.getElementById("usernameError").innerHTML = ajax.responseText;
                        }
                        else
                        {
                            document.getElementById("usernameError").innerHTML = "可用该用户名";
                        }
                    }
                    else
                    {
                        alert(ajax.status);
                    }
                }
            }
            ajax.open("POST","/ajax/ajaxRequestTest",true);
            ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            ajax.send("username=" + value);
        }
    }
</script>
<table align="center">
    <form>
        <tr align="center">
            <th>用户名</th>
            <th><input type="text" name="username" id="username"/></th>
            <th id="usernameError" style="color: red;"></th>
        </tr>
        <tr align="center">
            <th>邮箱</th>
            <th><input type="email" name="username" id="email"/></th>
        </tr>
        <tr align="center">
            <th>密码</th>
            <th><input type="password" name="username" id="password1"/></th>
        </tr>
        <tr align="center">
            <th>确认密码</th>
            <th><input type="password" name="username" id="password2"/></th>
        </tr>
        <tr>
            <th>登录或重置</th>
            <th>
                <input type="submit" name="submit" id="登录"/>
                <input type="reset" name="reset" id="重置"/>
            </th>
        </tr>
    </form>
</table>
<div id="myDiv"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值