JS屏蔽非法字符–表单提交

<script>

//计算字符长度

function checkStrLen(string){

var str,Num = 0;

for(var i=0;i<string.length;i++){

str = string.substring(i,i+1);

if(str<="~"){  //判断是否双字节

Num+=1;

}else{

Num+=3;

}

}

return Num;

}

function check_data(){

var userformat=/^(\w|[\u4E00-\u9FA5])+$/

var emailformat=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

var nostr="51cto|51ct0|root|妈的|傻逼|王八|admin|管理|bbs|blog|group";

var pattern = new RegExp(nostr,"gi");

//username

if(document.getElementById('username').value==""){
alert("用户名不能为空。");

document.getElementById('username').focus();

document.getElementById('checkname').value = "0";

return false;

} else if(checkStrLen(document.getElementById('username').value)>15 || checkStrLen(document.

getElementById('username').value)<4){
alert("用户名请填写4-15个字符长度的中文/字母/数字,同时请勿使用特殊字符。");

document.getElementById('username').focus();
document.getElementById('checkname').value = "0";

return false;

} else if(!userformat.test(document.getElementById('username').value)){

alert("用户名不能使用特殊字符。");

document.getElementById('username').focus();

document.getElementById('checkname').value = "0";

return false;

} else if(pattern.test(document.getElementById('username').value)){

alert("用户名包含敏感字符或被系统屏蔽。");

document.getElementById('username').focus();

return false;

} else if(document.getElementById('usernameexist').value==1){

alert("此用户名已经被注册,请更换。");

document.getElementById('username').focus();

return false;

} else {

document.getElementById('checkname').value = "1";

}

}
</script>

<form name="reg" action="" method="post" id="reg" onSubmit="return check_data()" autocomplete="off" >
<input type="hidden" name="reback" value="http://blog.haokaikai.cn">
<input type="hidden" name="fromuid" value="">
<input type="hidden" name="reg_logfrom" value="http://blog.haokaikai.cn">
<input type="hidden" name="checkname" id="checkname" value="0">
<input name="step" type="hidden" value="1">
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>
<td height="18" colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="180" align="right" valign="bottom" class="font1">用户名:&nbsp;<span  style=" color:#FF0000">*</span>&nbsp;</td>
<td width="2" rowspan="9" class="font2"></td>
<td width="440" class="font2"> <input name="username" id="username" type="text teat1_1"  class="teat1" size="15"    onChange="usercheck('check')" onBlur="usercheck('check')" value=""  > </td>
</tr>
<tr>
<td align="right" valign="bottom" class="font1">&nbsp;</td>
<td height="30" valign="top" class="font2"><span id="check">4-15个字符长度的中文/字母/数字,请勿使用特殊字符</span></td>
</tr>

</table>
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td height="1" colspan="2" bgcolor="#EBEBEB"></td>
</tr>
<tr>
<td height="22" colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交">&nbsp; <input type="button" value="重置"  onclick="javascript:document.all.reg.reset();"/></td>
</tr>
<tr>
<td height="22" colspan="2">&nbsp;</td>
</tr>
</table>
</form>

THE END
分享
二维码
< <上一篇
下一篇>>