检查值是否包含字符串字符

Check if a value contains a String character

本文关键字:字符串 字符 包含 是否 检查      更新时间:2023-09-26

现在我有了一个文本字段,在javascript中我想检查这个文本字段的值是否是或包含字符串。

我尝试过以下几种:

      if(isNaN(parseInt($(this).val()))){}

没有效果。

我也试过:

        if(typeof(parseInt($(this).val())) === "string"){}

尽管这两个例子似乎有效。如果你只是把第一个字符作为一个数字,那么其他的就无关紧要了。

所以我的问题是怎么做?

也许用正则表达式来检查值是否包含数字以外的任何内容:

/'D/.test(this.value)

FIDDLE

您可以将字符串拆分为一个字符数组,并分别进行检查:

var s = "11test";
var split_s = s.split("");
for(var index=0;index<split_s.length;index++) {
    if(isNaN(parseInt(split_s[index]))){
    }
}
  if(!isNaN(parseInt($(this).val())) && $(this).val().length==parseInt($(this).val()).length) {}
if(typeof($(this).val()) == "string"){//is a string
    //do something with string
}else if(typeof($(this).val()) == "object"){//is a object
    if($(this).val().length != undefined){//is a array
       //do something with array
    }else{//is a object
      //do something with json object
    }
}