网络编程
位置:首页>> 网络编程>> JavaScript>> 用JavaScript判断字符串长度

用JavaScript判断字符串长度

 来源:asp之家 发布时间:2009-10-29 12:15:00 

标签:JavaScript,字符串,长度

判断字符串长度函数:

<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 wsws totallength>20) 
  {
document.forma.sUserName.select();
   alert("用户名必须是3-20个字符!请重新输入。");
return  false;
}
return  true;
}
//-->
</SCRIPT>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com