注册验证代码整理(dwr+js)

百事通信息网


1、校验工具类
function ChkUtil() { }
//定位焦点
ChkUtil.onFocus = function (obj) {
 if (obj) {obj.focus();}
};
//删除串两边空格
ChkUtil.trim = function (str) {
 var patrn = /(^\s+|\s$)/g;
 return str.replace(patrn, "");
};
//校验是否为空(先删除二边空格再验证)
ChkUtil.isNull = function (str) {
 if (null == str ||  ""== ChkUtil.trim(str)) {
  return true;
 } else {
  return false;
 }
};
//校验是否全由数字组成
ChkUtil.isDigit = function (digit) {
 var patrn = /^[0-9]{1,20}$/;
 return patrn.test(digit);
};
//校验字符串:只能输入6-20个字母、数字、下划线
ChkUtil.isString6_20=function(str){
 var patrn=/^(\w){6,20}$/;
 return patrn.test(str);
};
//校验电话号码
ChkUtil.isMobile = function (mobile) {
 var patrn = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
 return patrn.test(mobile);
};
//校验电邮地址
ChkUtil.isEmail = function (email) {
 var patrn = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
 return patrn.test(email);
};

前端应用代码:
 <script type='text/javascript' src='../../dwr/interface/DWRUser.js'></script>
 <script type='text/javascript' src='../../dwr/engine.js'></script>
 <script type='text/javascript' src='../../dwr/util.js'></script>
 <script type='text/javascript' src='../js/chked.js'></script>
 <script type='text/javascript'>
//具体表单验证类
  var RegChked=function(){
      this.flg=true;
   this.userMsg="";
   this.pswdMsg="";
   this.userChk=function(username){
    if(!ChkUtil.isString6_20(username)){
     this.userMsg="用户名不正确!";
     this.flg=false;
    }else{
              DWRUser.isExist(username,callBack); 
    }
   };
   this.pswdChk=function(password,password2){
    if(!ChkUtil.isString6_20(password)){
     this.pswdMsg="密码不正确!";
     this.flg=false;
    }else{
              if(password!=password2){
               this.pswdMsg="密码不一致!";
               this.flg=false;
              }  
    }
   };
   var callBack=function(data){
    if(data){
     DWRUtil.setValue("userMsg","该用户名已经被注册!");
     this.flg=false; 
    }
   };
  }
//表单提交前调用函数(onsubmit="return onChked()")
  function onChecked(){
   var rc=new RegChked();
   rc.userChk($("username").value);
   rc.pswdChk($("password").value,$("password2").value);
   if(rc.flg){
    return true;
   }else{
    var errors={userMsg:rc.userMsg,pswdMsg:rc.pswdMsg};
    DWRUtil.setValues(errors);
    return false;
   }
  }
 </script>
<body>
 <html:form action="/userCenter?method=regedit" method="post" onsubmit="return onChked()" >
  <table border="0" align="center" width="500">
   <tr>
    <td width="100">用户名:</td>
    <td width="200">
     <html:text property="username"/>
    </td>
    <td>
     <div id="userMsg"></div>
    </td>
   </tr>
   <tr>
    <td>密码:</td>
    <td>
     <html:password property="password" />
    </td>
    <td>
     <div id="pswdMsg"></div>
    </td>
   </tr>
   <tr>
    <td>确认密码:</td>
    <td>
     <html:password property="password2" />
    </td>
    <td>
     <br>
    </td>
   </tr>
   <tr>
    <td colspan="2" align="center">
     <html:submit value=" 注册 " />
    </td>
    <td>
     <br>
    </td>
   </tr>
  </table>
 </html:form>
</body>


上述代码中存在异步问题,网上看了一些资料,有用信号量来同步的,不过用js信号量的算法怎么来定,没有头绪

你可能感兴趣的:(算法,DWR,mobile)