Home > Web Front-end > JS Tutorial > jQuery asynchronously validates whether username exists sample code_jquery

jQuery asynchronously validates whether username exists sample code_jquery

WBOY
Release: 2016-05-16 16:47:29
Original
1137 people have browsed it

Now there is a need to asynchronously verify whether the username exists. The technology used is jQuery asynchronous verification and struts2 (in fact, springMVC is the same, it is just a controller).

Form:

Copy code The code is as follows:

value="${dept.dept_name}" onchange="tocheckname()"/>

js code:
Copy the code The code is as follows:

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(0alert("This department already exists");
$("#dept_name").attr("value","");
$(" #dept_name").focus();
}
}
});
}

Backend code:
Copy code The code is as follows:

/**
* Query whether this department exists based on the department name
*
* @return
* @throws Exception
*/
public String checkdeptname() throws Exception {
if (UtilAPI.isNull(dept)) {
dept = new Dept();
}
int count = this.deptService.checkdeptname(dept.getId(), dept.getDept_name()); / /Returns 1 if it exists, returns 0 if it does not exist
try {
response.getOutputStream().print(count);
}
catch (IOException e) {
e.printStackTrace() ;
}
return Action.NONE;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template