<script src="<%=path%>/manage/js/prototype.js" type="text/javascript"></script>
<script language="javascript">
var flag=0;
function checkusername()
{
var username=document.getElementById("userName").value;
var usernameinfo=document.getElementById("userNameinfo");
if(username=="")
{
usernameinfo.innerHTML="<font color=red>不能为空</font>";
flag=0;
return false;
}
var url = "<%=path%>/manageuser.do?task=validate&userName="+username;
new Ajax.Request(url, { asynchronous:false, method:'post' ,
onComplete: function(xmlHttp)
{
if (200 == xmlHttp.status)
{
if(xmlHttp.responseText=="false")
{
usernameinfo.innerHTML="可以使用";
flag=1
return true;
}
else { usernameinfo.innerHTML="<font color=red>已经被占用</font>";
flag=0;
return false;
}
}else if(404 == xmlHttp.status){
alert("404");
}else if (500 == xmlHttp.status){
alert("500");
}
}});
}
</script>
<input type="text" name="userName" id="userName"οnblur="checkusername();" />
<span id="userNameinfo">
后台action处理
if ("validate".equals(task)) {//判断用户名是否重复
String flag = "false";
String userName = request.getParameter("userName");
Staff staff = new Staff();
staff.setCategoryId(tmpCurUser.getCategoryID());
staff.setUserName(userName);
Staff staffflag = staffDao.checkName(staff);
if (staffflag != null) {
flag = "true";
} else {
flag = "false";
}
try {
response.getWriter().print(flag);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
<script language="javascript">
var flag=0;
function checkusername()
{
var username=document.getElementById("userName").value;
var usernameinfo=document.getElementById("userNameinfo");
if(username=="")
{
usernameinfo.innerHTML="<font color=red>不能为空</font>";
flag=0;
return false;
}
var url = "<%=path%>/manageuser.do?task=validate&userName="+username;
new Ajax.Request(url, { asynchronous:false, method:'post' ,
onComplete: function(xmlHttp)
{
if (200 == xmlHttp.status)
{
if(xmlHttp.responseText=="false")
{
usernameinfo.innerHTML="可以使用";
flag=1
return true;
}
else { usernameinfo.innerHTML="<font color=red>已经被占用</font>";
flag=0;
return false;
}
}else if(404 == xmlHttp.status){
alert("404");
}else if (500 == xmlHttp.status){
alert("500");
}
}});
}
</script>
<input type="text" name="userName" id="userName"οnblur="checkusername();" />
<span id="userNameinfo">
后台action处理
if ("validate".equals(task)) {//判断用户名是否重复
String flag = "false";
String userName = request.getParameter("userName");
Staff staff = new Staff();
staff.setCategoryId(tmpCurUser.getCategoryID());
staff.setUserName(userName);
Staff staffflag = staffDao.checkName(staff);
if (staffflag != null) {
flag = "true";
} else {
flag = "false";
}
try {
response.getWriter().print(flag);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}