<input type="text" id="name" placeholder="请输入用户名" value="" onblur="CheckChinese('name',this.value)" class="form-control"> function CheckIsChinese(obj,val){ var reg = new RegExp("[\\u4E00-\\u9FFF]+","g"); if(reg.test(val)){alert("包含汉字!"); } } |
function chkstrlen(str){ var strlen = 0; for(var i = 0;i < str.length; i++){ if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2 strlen += 2; else strlen++; } return strlen; } |