<SCRIPT language=JavaScript> <!-- Begin//控制录入字数maxLen = 50; // max number of characters allowedfunction checkMaxInput(form) { if (form.title.value.length > maxLen) // if too long.... trim it! form.title.value = form.title.value.substring(0, maxLen); // otherwise, update 'characters left' counter else form.remLen.value = maxLen - form.title.value.length; } // End --> </SCRIPT> //在BODY里 <TEXTAREA cols=36 name=msword onkeydown=checkMaxInput1(this.form) onkeyup=checkMaxInput1(this.form) rows=6 wrap=VIRTUAL></TEXTAREA> <INPUT maxLength=4 name=remLen1 readOnly size=4 value=144>
不知道对IE和NS的版本要求是多少?
function clen(str1) dim WINNT_CHINESE WINNT_CHINESE=(len("风雨雷电堂")=5) if not isnull(str1) then if WINNT_CHINESE then dim l,t,c dim i l=len(str1) t=l for i=1 to l c=asc(mid(str1,i,1)) if c<0 then c=c+65536 if c>255 then t=t+1 end if next clen=t else clen=len(str1) end if else clen=0 end if end functionfunction cleft(str1,int1) dim WINNT_CHINESE WINNT_CHINESE=(len("长宇")=2) if WINNT_CHINESE then dim l,t,c dim i,j l=len(str1) t=0 j=0 i=1 do while i<=l c=asc(mid(str1,i,1)) if c<0 then c=c+65536 if c>255 then t=t+2 else t=t+1 end if if t>=int1 then j=i exit do end if i=i+1 loop cleft=left(str1,j) else cleft=left(str1,int1) end if end function 我可以倾我所有了,好歹给点分。
<!-- Begin//控制录入字数maxLen = 50; // max number of characters allowedfunction checkMaxInput(form) {
if (form.title.value.length > maxLen) // if too long.... trim it!
form.title.value = form.title.value.substring(0, maxLen);
// otherwise, update 'characters left' counter
else form.remLen.value = maxLen - form.title.value.length;
}
// End -->
</SCRIPT>
//在BODY里
<TEXTAREA cols=36 name=msword onkeydown=checkMaxInput1(this.form) onkeyup=checkMaxInput1(this.form) rows=6 wrap=VIRTUAL></TEXTAREA>
<INPUT maxLength=4 name=remLen1 readOnly size=4 value=144>
dim WINNT_CHINESE
WINNT_CHINESE=(len("风雨雷电堂")=5)
if not isnull(str1) then
if WINNT_CHINESE then
dim l,t,c
dim i
l=len(str1)
t=l
for i=1 to l
c=asc(mid(str1,i,1))
if c<0 then c=c+65536
if c>255 then
t=t+1
end if
next
clen=t
else
clen=len(str1)
end if
else
clen=0
end if
end functionfunction cleft(str1,int1)
dim WINNT_CHINESE
WINNT_CHINESE=(len("长宇")=2)
if WINNT_CHINESE then
dim l,t,c
dim i,j
l=len(str1)
t=0
j=0
i=1
do while i<=l
c=asc(mid(str1,i,1))
if c<0 then c=c+65536
if c>255 then
t=t+2
else
t=t+1
end if
if t>=int1 then
j=i
exit do
end if
i=i+1
loop
cleft=left(str1,j)
else
cleft=left(str1,int1)
end if
end function
我可以倾我所有了,好歹给点分。
<script>
String.prototype.NewLength=function() {return this.replace(/[^\x00-\xff]/g,"**").length}
alert("我来kkk!".NewLength())
</script>
不过有个问题,如果string的最前或者最后一个字符是"s",那么
<input id=test maxchar=10><input type=submit></form>
<script language="JavaScript">
function checkit(){
var len = 0;
str = frm.test.value;
for(var i=0; i<str.length;i++,len++)
if(str.charCodeAt(i)>255) len++;
if(len>frm.test.maxchar){
alert("too many characters"); frm.test.focus();return false;
}
return true;
}
</script>