struts2 ajax 驗證使用者名稱是否存在

來源:互聯網
上載者:User

struts2 ajax 驗證使用者名稱是否存在
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<!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>
</head>

<body>
<tr>
                            <td class="label">用&nbsp;&nbsp;戶&nbsp;名:</td>
                            <td><s:textfield onblur="checkusername(this,'/register!check.action')"  css教程class="inputstyle" name="user.name"/></td>
                            <td width="150px;"><span id="namemessage"></span><s:fielderror cssclass="filederrorclass" fieldname="name"/></td>
                        </tr>

</body>
</html>

xml文檔
<package name="ajaxuser" extends="網頁特效on-default">
        <action name="register" class="com.test.user.registeraction">
            <result name="input">/user/register.網頁特效p/jsp.html target=_blank >jsp教程</result>
            <result name="success">/user/register_message.jsp</result>
            <result name="myjson" type="json"></result>
        </action>
    </package>


action.代碼

public string check() throws exception {
        actioncontext ctx = actioncontext.getcontext();
        https教程ervletrequest request = (httpservletrequest)ctx.get(servletactioncontext.http_request);      
        string username = request.getparameter("inputname");
        if(checkuserexit(username)) {
            this.registered = true;
        }
        return "myjson";
    }

js代碼
var xmlhttp;
        var currentname;
 
        function createxmlhttprequest() {
            if (window.activexobject) {
                xmlhttp = new activexobject("microsoft.xmlhttp");
            }
            else if (window.xmlhttprequest) {
                xmlhttp = new xmlhttprequest();
            }
        }
       
        function checkusername(field,url) {
            var name = field.value;
            if(name=="" || name.length<3) {
                document.getelementbyid("namemessage").innerhtml="使用者名稱應該不小於3位";
                return;
            }else {
                createxmlhttprequest();
                url = url+"?inputname="+name;
                currentname = name;
                xmlhttp.open("get", url);
                xmlhttp.onreadystatechange = callback;
                xmlhttp.send(null);
            }
        }
       
        function callback() {
             if (xmlhttp.readystate == 4) {
                 if (xmlhttp.status == 200) {
                     var user = eval('(' + xmlhttp.responsetext + ')');
                     if(user.registered) {
                         document.getelementbyid("namemessage").innerhtml ="<font color='red'>"+currentname+", 已經被註冊 !</font>";
                     }else {
                         document.getelementbyid("namemessage").innerhtml = "<font color='green'>"+currentname+", 可以註冊 !</font>";
                     }
                 }
             }
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.