花了几个小时学习了用jquery写了个  验证密码是否可用的小例子

jsp代码:

  •  <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代码:

  •  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();
  • }
  • }