日期:2014-05-16  浏览次数:20417 次

javascript正则表达式验证用户名
要求用户名可以是数字、字母、汉字,其他的字符都不允许,怎么写?看书写了几次都不对

------解决方案--------------------
HTML code
<script type="text/javascript">
   var p = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\u4e00-\u9fa5]).*$/ 
   var arr=["asdf汉字","1a^","1A","11","1a2A","a1b2c汉字","a1汉字b2cA"]
   for(var i=0;i<arr.length;i++){
    document.write(arr[i].replace(" ","&nbsp;")+":"+p.test(arr[i])+"<br/>");
   }
</script>

------解决方案--------------------
/^[a-zA-Z0-9\u4E00-\u9FFF]{2,10}$/
------解决方案--------------------
一时搞错,再来

HTML code
<script type="text/javascript">
   var p = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\u4e00-\u9fa5])[\da-zA-Z\u4e00-\u9fa5]+$/ 
   var arr=["asdf汉字","1a^","1A","11","1a2A","a1b2c汉字","a1汉字b2cA!"]
   for(var i=0;i<arr.length;i++){
    document.write(arr[i].replace(" ","&nbsp;")+":"+p.test(arr[i])+"<br/>");
   }
</script>

------解决方案--------------------
哦,我那个是三者都一定要输入了,结合2楼的话用/^[a-zA-Z0-9\u4E00-\u9FFF]+$/吧
------解决方案--------------------
探讨
要求用户名可以是数字、字母、汉字,其他的字符都不允许,怎么写?看书写了几次都不对

------解决方案--------------------
^[0-9a-zA-z\u4E00-\u9FA5]+$