The character here refers to the Chinese character occupies two bytes, while the number and its letter occupy one byte. If substr is used, it is definitely not possible.
1. distinguish between two-byte characters and One-byte character;
2. Use charCodeAt and String. fromCharCode to convert each other.
The Code is as follows:
The Code is as follows:
Function validatePostponeValue (obj, objLength)
{
Var executeResult = false;
Var value = obj. value;
Var byteLen = 0, len = value. length;
Var newValue = "";
If (value)
{
For (var I = 0; I {
If (value. charCodeAt (I)> 255)
{
ByteLen + = 2;
If (byteLen <= 18)
{
// Alert (String. fromCharCode (value. charCodeAt (I )));
NewValue + = String. fromCharCode (value. charCodeAt (I ));
}
}
Else
{
ByteLen ++;
If (byteLen <= 19)
{
// Alert (String. fromCharCode (value. charCodeAt (I )));
NewValue + = String. fromCharCode (value. charCodeAt (I ));
}
}
}
}
If (byteLen <= 0)
{
// Alert ("cannot be blank! ");
Obj. focus ();
}
Else if (byteLen> objLength)
{
Alert ("A maximum of 10 Chinese characters (20 characters) can be entered ). ");
Obj. focus ();
Obj. value = newValue; // value. substr (0, objLength-1 );
}
Else
{
ExecuteResult = true;
}
Return executeResult;
}