简体   繁体   English

如何在JavaScript中检查字符串长度?

[英]How to check string length in JavaScript?

How can I check the length of in String in JavaScript? 如何在JavaScript中检查String中的长度? 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. 表达式'value.length!== 10'不起作用。 A String must contain at least 10 characters. 字符串必须包含至少10个字符。 How can I solve this problem? 我怎么解决这个问题?

可以使用适当的正则表达式\\d{10,}代替testmatch

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

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 希望这可以帮助

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM