您的位置:首页 > Web前端 > JavaScript

用JavaScript判断字符串长度

2005-04-12 10:25 344 查看
<SCRIPT LANGUAGE="JavaScript">
<!--
function a()
{
var totallength=0;
for (var i=0;i<document.forma.sUserName.value.length;i++)
{
var str=document.forma.sUserName.value;
var intCode=str.charCodeAt(i);
if (intCode>=0 && intCode<=128) {//continue;
totallength=totallength+1;
}
else {
totallength=totallength+2;
}
} //end for
alert(totallength);
if (totallength<3 || totallength>20)
{
document.forma.sUserName.select();
alert("用户名必须是3-20个字符!请重新输入。");
return false;
}
return true;
}
//-->
</SCRIPT>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: