user2504767
user2504767

Reputation:

How to check string length in JavaScript?

How can I check the length of in String in JavaScript? Here is a small code example:

if(value != null && value != "" && value.length !== 10 && !value.match(/^\d*$/)){

   // do something

}

The expression 'value.length !== 10' doesn´t work. A String must contain at least 10 characters. How can I solve this problem?

Upvotes: 2

Views: 20168

Answers (3)

Jeam Sedric Espinosa
Jeam Sedric Espinosa

Reputation: 327

To Get the string length of a value for example:

var value="This is a string";
var string_length=value.length;
/*The string length will result to 16*/

Hope this helps

Upvotes: 4

Tushar
Tushar

Reputation: 87203

Instead of match, test can be used with proper regex \d{10,}.

if (value && /^\d{10,}$/.test(value.trim()))

Upvotes: 7

user4090029
user4090029

Reputation:

var regex = new RegExp(/^\d*$/),
    value = $.trim("1234567890");

if (value.length >= 10 && regex.exec(value)) {
   // correct
} else {
   // incorrect
}

Upvotes: 0

Related Questions