$(function(){
//獲取input標籤
$("#username").blur(function(){//給用戶名綁定一個離焦事件
// alert(000);
//獲取用戶輸入的信息
var username = $(this).val();
// alert(username);字母 數字 下劃線
if( !username.match("^\\w{4,12}$")){
//如果用戶輸入的不符合校驗就給用戶一個錯誤提示。把錯誤提示寫到span裡
$("#username_res").html("用戶名必須由4~12位字母,數字,下劃線組成");
}else{
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/demo",
data: "username="+username,
success: function(data){
$("#username_res").html(data);
}
});
}
});
});
//清楚請輸入用戶名
function clearUserName(){
//獲取input標籤
var input_name = document.getElementById("username");
//判斷value值裡的內容(請輸入用戶名)
if(input_name.value == "請輸入用戶名"){
input_name.value="";
}
}
1.7.2.Servlet代碼:
閱讀更多 野生java工程師 的文章