One Chinese character is recognized as 2
Check whether the value is smaller than the specified length
Varfield Value
Limit_len Length
Return Value: Number
If> = 0
- Function getleftchars (varfield, limit_len)
- {
- VaR I = 0;
- VaR counter = 0;
- VaR Cap = limit_len;
- VaR J = 0;
- For (I = 0; I <varfield. value. length; I ++)
- {
- If (varfield. value. charcodeat (I)> 127 | varfield. value. charcodeat (I) = 94)
- {
- J = J + 2;
- }
- Else
- {
- J = J + 1
- }
- } // End the For Loop
- // Var leftchars = CAP-varfield. value. length;
- VaR leftchars = CAP-J;
- Return (leftchars );
- }
- Function oncharschange (varfield, a_str, a_limit)
- {
- VaR leftchars = getleftchars (varfield, a_limit );
- If (leftchars> = 0)
- {
- Return true;
- }
- Else
- {
- Ls_str = a_str + "the number of words exceeds the limit (" + a_limit +! ";
- Window. Alert (ls_str );
- // Var Len = Document. thform. Ti. value. Length + leftchars;
- // Document. login. Message. value = Document. login. Ti. value. substring (0, Len );
- Varfield. value = ""
- Return false;
- }
- }
- <Input type = "text" name = "un" size = 15 onblur = "oncharschange (this, 'username', 20)" onchange = "oncharschange (this, 'username ', 20) ">
From: http://www.yxl.cn/Info/20051231,124412,2987.html