免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2029 | 回复: 1
打印 上一主题 下一主题

JS验证 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-13 21:26 |只看该作者 |倒序浏览
JS验证










其实写服务器和写客户端是一样的,除了技术的支持最重要的还是对业务流程的熟悉!当然在客户端更多的是对用户动作和事件的清晰,下面就是一个用户更新个人资料的简单验证.




Js代码
  1. 1.<script language="javascript" type="text/javascript">  
  2. 2.    hostURl ="<%=base%>";  
  3. 3.  
  4. 4.  
  5. 5.//客户端检查基本的邮箱信息  
  6. 6.function checkClientEmail(userEmail){  
  7. 7.    var emailReg = new RegExp("^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$");  
  8. 8.    if(userEmail.trim()==""){  
  9. 9.        $("#emailTip").html("<font color='red'>請輸入郵箱</font>");  
  10. 10.        $("#userEmail").focus();  
  11. 11.        return false;  
  12. 12.    }else{  
  13. 13.        if(emailReg.test(userEmail)){  
  14. 14.            $("#emailTip").html("");  
  15. 15.            return true;  
  16. 16.        }else{  
  17. 17.            $("#emailTip").html("<font color='red'>請正确輸入郵箱</font>");  
  18. 18.            $("#userEmail").focus();  
  19. 19.            return false;  
  20. 20.        }  
  21. 21.    }  
  22. 22.}  
  23. 23.  
  24. 24.//服务器端检查该邮箱是否存在  
  25. 25.function checkServerEmail(userEmail){  
  26. 26.    var isServerExisted=true;  
  27. 27.    $.ajax( {  
  28. 28.        url : hostURl + '/user/checkEmail.action',  
  29. 29.        type : "post",  
  30. 30.        data : {  
  31. 31.            email : userEmail  
  32. 32.        },  
  33. 33.        dataType : 'text',  
  34. 34.        error : function() {  
  35. 35.            $("#emailTip").html("<font color='red'>請求出錯,請稍後再試</font>");  
  36. 36.            $("#userEmail").focus();  
  37. 37.        },  
  38. 38.        success : function(data) {  
  39. 39.            var ret = eval("(" + data + ")");  
  40. 40.            if (ret.msg == "success") {  
  41. 41.                isServerExisted=false;  
  42. 42.                $("#userEmail").attr("class","unchangeable").attr("readonly","readonly");  
  43. 43.                $("#emailTip").html("<font color='green'>該郵箱可以使用</font>");  
  44. 44.            }else {  
  45. 45.                $("#emailTip").html("<font color='red'>該郵箱已經存在,請輸入其它郵箱</font>");  
  46. 46.                $("#userEmail").focus();  
  47. 47.            }  
  48. 48.        }  
  49. 49.    });  
  50. 50.    return isServerExisted;  
  51. 51.}  
  52. 52.  
  53. 53.//当邮箱检查通过后检查其它字段信息  
  54. 54.function submitUserInfoByCheck(){  
  55. 55.  
  56. 56.    //普通字段  
  57. 57.    var userId=$("#userId").val().trim();  
  58. 58.    var userRealName=$("#userRealName").val().trim();  
  59. 59.    var userGender=$("input[name='user.gender']:checked").val();  
  60. 60.    var userInterest=$("#userInterest").val().trim();  
  61. 61.    var userAddress=$("#userAddress").val().trim();  
  62. 62.    var userEmail=$("#userEmail").val().trim();  
  63. 63.    var userPhone=$("#userPhone").val().trim();  
  64. 64.  
  65. 65.    //必须的字段  
  66. 66.    var userPassword=$("#userPassword").val().trim();  
  67. 67.    var userScreenName=$("#userScreenName").val().trim();  
  68. 68.  
  69. 69.    //单独提取出来的日期类型的生日  
  70. 70.    var userBirthday=$("#userBirthday").val().trim();  
  71. 71.      
  72. 72.    if(userScreenName=="") {  
  73. 73.        $("#screenNameTip").html("<font color='red'>請輸入昵稱后再提交</font>");  
  74. 74.        $("#userScreenName").focus();  
  75. 75.        return false;  
  76. 76.    }  
  77. 77.    if(userPassword==""){  
  78. 78.        $("#userPasswordTip").html("<font color='red'>請輸入密碼后再提交</font>");  
  79. 79.        $("#userPassword").focus();  
  80. 80.        return false;  
  81. 81.    }else if(userPassword.length <6) {  
  82. 82.        $("#userPasswordTip").html("<font color='red'>輸入的密碼長度不能小於6位</font>");  
  83. 83.        $("#userPassword").focus();  
  84. 84.        return false;  
  85. 85.    }  
  86. 86.  
  87. 87.    //封装user对象属相让struts自动填充,让birthday单独处理  
  88. 88.    var userAttrs="user.id="+userId+"&user.name="+userRealName+"&user.userName="+userScreenName+"&user.gender="  
  89. 89.                +userGender+"&user.interest="+userInterest+"&user.address="+userAddress+"&user.email="+userEmail  
  90. 90.                +"&user.phone="+userPhone+"&user.password="+userPassword;  
  91. 91.    $.ajax( {  
  92. 92.        url : hostURl + '/userCenter/updateUserselfModifyInfo!updateUserselfModifyInfo.action?'+userAttrs,  
  93. 93.        type : "post",  
  94. 94.        data : {  
  95. 95.            userBirthday : userBirthday  
  96. 96.        },  
  97. 97.        dataType : 'json',  
  98. 98.        error : function() {  
  99. 99.            alert("抱歉,服務器忙,請您稍後再試!");  
  100. 100.        },  
  101. 101.        success : function(data) {  
  102. 102.            if (data.status) {  
  103. 103.                alert("修改信息成功");  
  104. 104.                window.location.href = hostURl + "/userCenter/diplayUserInfo!getUserInfo.action";  
  105. 105.            }else {  
  106. 106.                alert("抱歉,服務器忙,請您稍後再試!");  
  107. 107.            }  
  108. 108.        }  
  109. 109.    });  
  110. 110.  
  111. 111.}  
  112. 112.  
  113. 113.//基本信息验证结束后就提交  
  114. 114.function updateUserInfo(){  
  115. 115.    var isClientChecked;  
  116. 116.    var isServerExisted;  
  117. 117.    var emailType=$("#userEmail").attr("class");  
  118. 118.    if(emailType=="changeable"){  
  119. 119.        var userEmail=$("#userEmail").val().trim();  
  120. 120.        isClientChecked=checkClientEmail(userEmail);  
  121. 121.        if(isClientChecked){  
  122. 122.            isServerExisted=checkServerEmail(userEmail);  
  123. 123.            if(!isServerExisted){  
  124. 124.                submitUserInfoByCheck();  
  125. 125.            }  
  126. 126.        }  
  127. 127.    }else{  
  128. 128.        submitUserInfoByCheck();  
  129. 129.    }  
  130. 130.}  
  131. 131.</script>  
复制代码
Html代码
  1. 1.<div>  
  2. 2.<h2>基本資料</h2>  
  3. 3.<input id="userId" type="hidden" name="user.id" value="${user.id}" />  
  4. 4.<ul>  
  5. 5.  
  6. 6.    <li>姓 名:<input id="userRealName" type="text" name="user.name" value="${user.name}" /></li>  
  7. 7.    <li>昵 稱:<input id="userScreenName" type="text" name="user.userName" value="${user.userName}" />&nbsp;<font color="red">*</font></li>  
  8. 8.    <li style="margin-left: 30px;" id="screenNameTip"></li>  
  9. 9.    <li>密 碼:<input id="userPassword" type="password" name="user.password" value="${user.password}" />&nbsp;<font color="red">*</font></li>  
  10. 10.    <li style="margin-left: 30px;" id="userPasswordTip"></li>  
  11. 11.    <s:if test="user.gender==1">  
  12. 12.        <li>性 别: <input type="radio" name="user.gender" value="1" checked="checked" />男   
  13. 13.                    <input type="radio" name="user.gender" value="2" />女   
  14. 14.                    <input type="radio" name="user.gender" value="0" />不公開  
  15. 15.        </li>  
  16. 16.    </s:if>  
  17. 17.    <s:elseif test="user.gender==2">  
  18. 18.        <li>性 别:<input type="radio" name="user.gender" value="1" />男   
  19. 19.                <input type="radio" name="user.gender" value="2" checked="checked" />女   
  20. 20.                <input type="radio" name="user.gender" value="0" />不公開  
  21. 21.        </li>  
  22. 22.    </s:elseif>  
  23. 23.    <s:else>  
  24. 24.        <li>性 别: <input type="radio" name="user.gender" value="1" />男  
  25. 25.                     <input type="radio" name="user.gender" value="2" />女   
  26. 26.                     <input type="radio" name="user.gender" value="0" checked="checked" />不公開  
  27. 27.        </li>  
  28. 28.    </s:else>  
  29. 29.  
  30. 30.    <li>生 日:<input id="userBirthday" type="text" name="user.birthday" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="readonly"  
  31. 31.        value="<s:date name="user.birthday" format="yyyy-MM-dd" />" />  
  32. 32.    </li>  
  33. 33.    <li>興 趣:<input id="userInterest" type="text" name="user.interest" value="${user.interest}" /></li>  
  34. 34.</ul>  
  35. 35.</div>  
复制代码
服务器端的处理



Java代码
  1. 1./**
  2. 2. * 将用户生日单独提取出来,<BR>
  3. 3. * 因为之前在本地使用struts自动封装没有问题,但是在远程服务器上总是报错<BR>
  4. 4. * 现在的做法就是让其它字段自动封装,而生日单独提取出来<BR>
  5. 5. * */  
  6. 6.public String updateUserselfModifyInfo() {  
  7. 7.  
  8. 8.    JSONObject resJson = new JSONObject();  
  9. 9.  
  10. 10.    String userBirthday = super.getRequest().getParameter("userBirthday");  
  11. 11.    Date birthday = null;  
  12. 12.    if (userBirthday != null && userBirthday.length() > 0)  
  13. 13.        birthday = dateUtil.parseStringToDate(userBirthday, "yyyy-MM-dd");  
  14. 14.  
  15. 15.    user.setBirthday(birthday);  
  16. 16.    String updateStatus = userService.updateUserselfModifyInfo(user);  
  17. 17.    if (updateStatus == null) {  
  18. 18.        resJson.put("status", true);  
  19. 19.        user = userService.getUserInfoById(user.getId());  
  20. 20.        super.getRequest().getSession().setAttribute(  
  21. 21.                CommonConstants.SESSION_USER, user);  
  22. 22.    } else  
  23. 23.        resJson.put("status", false);  
  24. 24.  
  25. 25.    super.ajaxJson(resJson.toString());  
  26. 26.  
  27. 27.    return null;  
  28. 28.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-24 19:58 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP