Thursday, October 18, 2007

Get number of characters in Javascript

How to count number of characters in a string in Javascript

length javascript's function returns the length (number of characters) of a given string.

For example:



var mystring = 'test string';

alert( mystring.length ) ;


This will alert a window with the number of characters of mystring variable.

If you want to get number of characters in a textfield using javascript, that you need to call the same function, length this way:


alert( document.yourform.yourfieldname.value.length );

No comments: