当前位置: 动力学知识库 > 问答 > 编程问答 >

character count using jquery

问题描述:

How can i count the number of characters in a textbox using jquery?

$("#id").val().length < 3

just counts upto 3 character spaces but not the number of characters

网友答案:

For length including white-space:

$("#id").val().length

For length without white-space:

$("#id").val().replace(/ /g,'').length

For removing only beginning and trailing white-space:

$.trim($("#test").val()).length

For example, the string " t e s t " would evaluate as:

//" t e s t "
$("#id").val(); 

//Example 1
$("#id").val().length; //Returns 9
//Example 2
$("#id").val().replace(/ /g,'').length; //Returns 4
//Example 3
$.trim($("#test").val()).length; //Returns 7

Here is a demo using all of them.

分享给朋友:
您可能感兴趣的文章:
随机阅读: