如何检查javascript变量中的值是否包含数字

How to check value in javascript variable for include number or not?

本文关键字:是否 数字 包含 变量 javascript 何检查 检查      更新时间:2023-09-26

如何检查javascript变量中的值是否包含数字?

例如:var test = "abcd1234";

这个变量CCD_ 2包括小写和数字。

如何使用javascript检查var值是否包含数字?

我尝试使用isNaN功能

var test = "abcd1234";
var test = isNaN(test);
if(test === true)
{ alert("not include number");
else
{ alert("include number");

但是这个代码提醒not include number,因为isNaN会检查var的所有数据,但我只想检查var的一部分。我该怎么做?

您必须使用regex。

var test = /'d/.test("abcd1234");
if (test === true) {
  alert("include number");
} else {
  alert("not include number");
}