//计算字符串长度(可同时字母和汉字,字母占一个字符,汉字占2个字符)
- function?strlen(str){??
- ????var?len?=?0;??
- ????for?(var?i=0;?i<str.length;?i++)?{???
- ?????var?c?=?str.charCodeAt(i);???
- ????//单字节加1???
- ?????if?((c?>=?0x0001?&&?c?<=?0x007e)?||?(0xff60<=c?&&?c<=0xff9f))?{???
- ???????len++;???
- ?????}???
- ?????else?{???
- ??????len+=2;???
- ?????}???
- ????}???
- ????return?len;??
- }??
?
?
?
/////////////////////////////////////////////////////////////////////?
//?? 判断长度是否合格?
//?
// 引数 s?? 传入的字符串?
//?????????? n?? 限制的长度n以下?
//?
// 返回值 false?? NG?
//?????????? true????OK?
/////////////////////////////////////////////////////////////////////?
- function?WidthCheck(s,?n){???
- var?w?=?0;???
- for?(var?i=0;?i<s.length;?i++)?{???
- ???var?c?=?s.charCodeAt(i);???
- ???//单字节加1???
- ???if?((c?>=?0x0001?&&?c?<=?0x007e)?||?(0xff60<=c?&&?c<=0xff9f))?{???
- ????w++;???
- ???}???
- ???else?{???
- ????w+=2;???
- ???}???
- }???
- if?(w?>?n)?{???
- ???return?false;???
- }???
- return?true;???
- } ?