求一段ajax 验证表单用户名是否存在的代码 jsp的 数据库是mysql 谢谢

2025-01-06 15:39:33
推荐回答(2个)
回答1:

1.
2.
3.
4.var flag="true"; //判断用户名称是否重复
5.
6.function validatorloginName(){
7.
8. var loginName=document.getElementById("loginName").value;
9. $j.ajax({ type: "POST",
10. url: "${pageContext.request.contextPath}/validatorLoginName.do",
11. data: "loginName="+loginName,
12. async: false ,
13. success: function(data){
14. if(data=="true"){
15. errormessage.innerHTML="用户名可用"
16. flag=true;
17. }else{
18. errormessage.innerHTML="用户名已存在";
19. flag=false;
20. }
21. }
22. });
23.}
24.
25.function mysubmit(){
26.
27. if(flag=="true"){
28. alert("用户名已经存在或输入格式不正确");
29. }else{
30. document.getElementById("myform").submit();
31. }
32.
33.}
34.
35.
36.
37.
38.
39.


40.
41. 42. name="loginName" onblur="validatorloginName()">
43.
44.
45.
46.
47.
48.
49.
50.
51.
1.public class ValidatorLoginNameAction extends org.springframework.web.struts.ActionSupport {
2. /*
3. * Generated Methods
4. */
5.
6. /**
7. * Method execute
8. * @param mapping
9. * @param form
10. * @param request
11. * @param response
12. * @return ActionForward
13. */
14. public ActionForward execute(ActionMapping mapping, ActionForm form,
15. HttpServletRequest request, HttpServletResponse response) throws Exception{
16. // TODO Auto-generated method stub
17. System.out.println("-------");
18. IRegisterManagerService registerManager = (IRegisterManagerService) this.getService("registerManagerServiceImpl");
19. String loginName=request.getParameter("loginName");
20.
21. boolean bool=registerManager.validatorName(loginName);//通过Service验证该用户是否存在
22.
23. if(bool==true){
24. response.getWriter().write("true");//此值jquery可以接收到
25. }else{
26.
27. response.getWriter().write("false");
28. }
29. return null;
30. }
31. /**
32. *


33. * 获取业务服务对象;
34. *


35. *
36. * @param serviceKey
37. * 服务对象ID;
38. * @return
39. */
40. public Object getService(String serviceKey) {
41. return this.getWebApplicationContext().getBean(serviceKey);
42. }
43.
44. protected static Logger getLog() {
45. return Logger.getInstance();
46. }
47.}

回答2:

jsp不会 呵呵