Reputation:
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
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
Reputation: 87203
Instead of match
, test
can be used with proper regex \d{10,}
.
if (value && /^\d{10,}$/.test(value.trim()))
Upvotes: 7
Reputation:
var regex = new RegExp(/^\d*$/),
value = $.trim("1234567890");
if (value.length >= 10 && regex.exec(value)) {
// correct
} else {
// incorrect
}
Upvotes: 0