JQuery asynchronously verifies that the sample code exists for the username

  • 2020-03-30 03:01:00
  • OfStack

One requirement now is to asynchronously verify that the username exists. The techniques used are jQuery asynchronous validation and struts2 (the same goes for springMVC, which is just a controller).

Form:
 
<input class="width150" maxlength="32" type="text" id="dept_name" name="dept.dept_name" 
value="${dept.dept_name}" onchange="tocheckname()"/> 

Js code:
 
function tocheckname() 
{ 
var deptName= $("#dept_name").val(); 
$.ajax({ 
type:"POST", 
cache:false, 
url : "${rootPath}/dept/checkdeptname.htm", 
dataType : "text", 
data:{"dept.id":"${dept.id}","dept.dept_name":deptName}, 
async:false, 
success : function(data){ 
if(0<parseInt(data)){ 
alert(" This section already exists "); 
$("#dept_name").attr("value",""); 
$("#dept_name").focus(); 
} 
} 
}); 
} 

Background code:
 
 
public String checkdeptname() throws Exception { 
if (UtilAPI.isNull(dept)) { 
dept = new Dept(); 
} 
int count = this.deptService.checkdeptname(dept.getId(), dept.getDept_name()); //If there is a return 1, there is no return 0
try { 
response.getOutputStream().print(count); 
} 
catch (IOException e) { 
e.printStackTrace(); 
} 
return Action.NONE; 
} 

Related articles: