How to validate multiple bytes data?

Discussion in 'JavaScript' started by lemonwlf, Dec 20, 2005.

  1. #1
    Does anyone knows how to check the number of byte for a string? In my case, the maximun size of character in db is 255 and i have tried to check the length of character myString.length. Unfortunately, user sometimes input chinese character and make the size is larger than that of database column(one chinese character represent 2 bytes). So, i would like to know how to check the number of byte for a string. If u have another methods to deal with this problem, it is welcome to reply. Thank a lot.
     
    lemonwlf, Dec 20, 2005 IP
  2. Cybernaut

    Cybernaut Peon

    Messages:
    408
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I will suggest something like this:

    function getNumBytes(s)
    {
    	var numBytes=0;
    	for (var i=0; i<s.length; i++)
    	{
    		var code=s.charCodeAt(i);
    		if (code < 0x0100)
    		{
    			numBytes+=1; // single byte
    		}
    		else
    		{
    			numBytes+=2; // double byte
    		}
    	}
    	return numBytes;
    }
    Code (markup):
    Get number of bytes by getNumBytes(myString). I hope its helpful.
     
    Cybernaut, Dec 20, 2005 IP
  3. lemonwlf

    lemonwlf Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am sincerely thanks to you. This solution is very useful for me to deal with a problem. Besides, i would like to know any document does describe what wording of 0x100 or what codeChartAt(another control character) such as codeChartAt(enter). Thanks
     
    lemonwlf, Dec 21, 2005 IP
  4. Cybernaut

    Cybernaut Peon

    Messages:
    408
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Chect out ascii tables http://www.lookuptables.com/

    Also,

    Enter: "\r".charCodeAt(0): 0x0013
    New Line: "\n".charCodeAt(0): 0x0010
    Tab: "\t".charCodeAt(0): 0x0009
    Backspace: "\b".charCodeAt(0): 0x0008
     
    Cybernaut, Dec 22, 2005 IP
  5. lemonwlf

    lemonwlf Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    For the control character, the charCodeAt(i) this character is lesser than 0x0100. Based on the previous check bytes algorithm, lesser than 0x0100 will be counted to 2 bytes. Is it correct for counting control character to be 2 bytes by using with charCodeAt method.
     
    lemonwlf, Jan 3, 2006 IP