js 判断是否是数字

js yekong

判断是否为纯数字

let a = 11
console.log(Number.isFinite(a))
console.log(Number.isFinite('wanjunshijie'))
console.log(Number.isFinite(NaN))
console.log(Number.isFinite(undefined))

结果

true
false
false
false

正则判断是否为数字

val = "123456"
var isnum = /^\d+$/.test(val);
document.write(isnum);
document.write("<br>");

val2 = "as123"
var isnum2 = /^\d+$/.test(val2);
document.write(isnum2);

结果

true
false

使用

newMoney: function() {
				var name = ''
				if (/^\d+$/.test(this.money)) {
					name = this.money + "元"
				} else {
					name = this.money
				}
				return name
			}
喜欢