我希望有与旧的VB6 IsNumeric()
函数在同一概念空间中的东西?
我希望有与旧的VB6 IsNumeric()
函数在同一概念空间中的东西?
我在Angular中使用了这个函数
isNumeric(value: string): boolean {
let valueToNumber = Number(value);
var result = typeof valueToNumber == 'number' ;
if(valueToNumber.toString() == 'NaN')
{
result = false;
}
return result;
}
我的解决方案:
// returns true for positive ints;
// no scientific notation, hexadecimals or floating point dots
var isPositiveInt = function(str) {
var result = true, chr;
for (var i = 0, n = str.length; i < n; i++) {
chr = str.charAt(i);
if ((chr < "0" || chr > "9") && chr != ",") { //not digit or thousands separator
result = false;
break;
};
if (i == 0 && (chr == "0" || chr == ",")) { //should not start with 0 or ,
result = false;
break;
};
};
return result;
};
你可以在循环内添加附加条件,以满足你的特定需求。
function isStringNumeric(str_input){
//concat a temporary 1 during the modulus to keep a beginning hex switch combination from messing us up
//very simple and as long as special characters (non a-z A-Z 0-9) are trapped it is fine
return '1'.concat(str_input) % 1 === 0;}
警告:这可能是利用Javascript和Actionscript中长期存在的一个漏洞 [Number("1" + the_string) % 1 === 0],我不能确定,但这正是我们所需要的。
% 1
操作),这将把字符串解释为十六进制或浮点字面量。 - Bergi我尝试了一个有点混乱的解决方案,也许不是最好的选择。
function isInt(a){
return a === ""+~~a
}
console.log(isInt('abcd')); // false
console.log(isInt('123a')); // false
console.log(isInt('1')); // true
console.log(isInt('0')); // true
console.log(isInt('-0')); // false
console.log(isInt('01')); // false
console.log(isInt('10')); // true
console.log(isInt('-1234567890')); // true
console.log(isInt(1234)); // false
console.log(isInt('123.4')); // false
console.log(isInt('')); // false
// other types then string returns false
console.log(isInt(5)); // false
console.log(isInt(undefined)); // false
console.log(isInt(null)); // false
console.log(isInt('0x1')); // false
console.log(isInt(Infinity)); // false
042
)和十六进制(0x45f
)。 - Domivar isNumber = (function () {
var isIntegerTest = /^\d+$/;
var isDigitArray = [!0, !0, !0, !0, !0, !0, !0, !0, !0, !0];
function hasLeading0s (s) {
return !(typeof s !== 'string' ||
s.length < 2 ||
s[0] !== '0' ||
!isDigitArray[s[1]] ||
isIntegerTest.test(s));
}
var isWhiteSpaceTest = /\s/;
return function isNumber (s) {
var t = typeof s;
var n;
if (t === 'number') {
return (s <= 0) || (s > 0);
} else if (t === 'string') {
n = +s;
return !((!(n <= 0) && !(n > 0)) || n === '0' || hasLeading0s(s) || !(n !== 0 || !(s === '' || isWhiteSpaceTest.test(s))));
} else if (t === 'object') {
return !(!(s instanceof Number) || ((n = +s), !(n <= 0) && !(n > 0)));
}
return false;
};
})();
判断一个字符串或者数字是否为数字
const isNumeric = stringOrNumber =>
stringOrNumber == 0 || !!+stringOrNumber;
或者,如果您想将字符串或数字转换为数字
const toNumber = stringOrNumber =>
stringOrNumber == 0 || +stringOrNumber ? +stringOrNumber : NaN;
我不喜欢提供的解决方案,因为我只需要数字。
nums = new Set(['1','2','3','4','5','6','7','8','9','0']);
hasOnlyNumbers= str =>[...str].every( x=>nums.has(x) );
https://jsfiddle.net/gaby_de_wilde/05fb7k3g/
如果你想允许负数(忽略字符串开头的负号)
nums = new Set(['1','2','3','4','5','6','7','8','9','0']);
hasOnlyNumbers=str=>[...str[0]=='-'?str.substring(1):str].every(x=>nums.has(x));
https://jsfiddle.net/gaby_de_wilde/y4fdwvxk/
如果您想让它更易读:nums = new Set(['1','2','3','4','5','6','7','8','9','0']);
hasOnlyNumbers = function(str){
if(str[0]=='-'){
str = str.substring(1)
}
return [...str].every(x=>nums.has(x));
}
https://jsfiddle.net/gaby_de_wilde/6a5qwx2t/
允许一个小数点(最多允许1个)
nums = new Set(['.','1','2','3','4','5','6','7','8','9','0']);
hasOnlyNumbers=str=>str.split('.').length<3&&[...str[0]=='-'?str.substring(1):str].every(x=>nums.has(x));
https://jsfiddle.net/gaby_de_wilde/hnx8d6so/
更易读:
nums = new Set(['.','1','2','3','4','5','6','7','8','9','0']);
hasOnlyNumbers = function(str){
if(str.split('.').length > 2) {
return false
}
if(str[0]=='-'){
str = str.substring(1)
}
return [...str].every(x=>nums.has(x));
}
注意:现在集合也有一个点。只需使用isNaN()
,它会将字符串转换为一个数字,如果获得一个有效的数字,就会返回false
...
isNaN("Alireza"); //return true
isNaN("123"); //return false
if (mystring > 0) ...
const isNumber = s => !isNaN(+s)
1..1
、1,1
、-32.1.12
等情况下会失败,更重要的是,它无法处理 undefined
和 NaN
。如果你传递了 undefined
或 NaN
,它会返回一个错误的结果,说它是一个数字。 - Jeremy
isNaN("")
、isNaN(" ")
、isNaN(false)
等情况下失败了。它返回false
,这意味着它们是数字。 - Andrew