花了幾個小時學習了用jquery寫了個 驗證密碼是否可用的小例子css
jsp代碼:jquery
- <form action="<%=request.getContextPath() %>/sys/userinfoJson.action" method="post" id="infoForm" theme="simple">
- <table width="100%" class="formtable">
- <s:hidden name="bean.userId" id="userid"></s:hidden>
- <s:hidden name="actiontype"></s:hidden>
- <tr>
- <td width="6px"><nobr>原始密碼:</nobr></td>
- <td><input type="password" maxlength="50" cssClass="x-form-text" name="oldpasswd" id="oldpwd" cssStyle="width:100%" style="width:100%" onblur="checkpwd()"/></td>
- <td width="12px" id="temp"><nobr><font color="red">*</font></nobr></td>
- </tr>
- <tr>
- <td width="6px"><nobr>新密碼:</nobr></td>
- <td><input type="password" maxlength="50" cssClass="x-form-text" name="bean.loginpwd" id="newpwd" cssStyle="width:100%" style="width:100%" o/> </td>
- <td width="12px"><nobr><font color="red">*</font></nobr></td>
- </tr>
- <tr>
- <td width="6px"><nobr>確認密碼:</nobr></td>
- <td><input type="password" maxlength="50" cssClass="x-form-text" name="confirmpasswd" id="repwd" cssStyle="width:100%" style="width:100%"/> </td>
- <td width="12px"><nobr><font color="red">*</font></nobr></td>
- </tr>
- </table>
- </form>
jquey代碼:ajax
- function checkpwd(){
- var userid = document.getElementById("userid").value;
- //alert(userid.value);
- jQuery(function(){
- var pwd= $.trim($("#oldpwd").val());//得到表單的值.
- $.ajax({
- url:"<%=request.getContextPath()%>/sys/checkpwd.action?userId="+userid, //請求服務器url地址.
- data:{password:pwd},//得到表單裏面的值,傳入服務器中..
- cache:false,
- success:function(response){
- if(response=="false"){
- document.getElementById('oldpwd').value = '';
- document.getElementById('temp').innerHTML='<nobr><font color="red">原始密碼錯誤!</font></nobr>';
- }else{
- document.getElementById('temp').innerHTML='<nobr><font color="#228B22">原始密碼可用</font></nobr>';
- }
- }
- })
- });
- }
處理代碼:服務器
- public void checkPwd(){
- HttpServletRequest request = getRequest();
- String id = request.getParameter("userId");
- bean = (SysUserEntity) getBaseService().getObject(bean.getClass(), id);
- String password = request.getParameter("password");
- try {
- PrintWriter out = getResponse().getWriter();
- if(password.equals(bean.getLoginpwd())){
- out.write("true");
- }else{
- out.write("false");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }