检查字符串是否只包含数字

发布于 2024-08-12 06:08:55 字数 312 浏览 2 评论 0原文

我想检查 string 是否仅包含数字。我使用了这个:

var isANumber = isNaN(theValue) === false;

if (isANumber){
    ..
}

但意识到它还允许 +-。基本上,我想确保输入仅包含数字而不包含其他字符。由于 +100-5 都是数字,因此 isNaN() 不是正确的方法。 也许正则表达式就是我需要的?有什么建议吗?

I want to check if a string contains only digits. I used this:

var isANumber = isNaN(theValue) === false;

if (isANumber){
    ..
}

But realized that it also allows + and -. Basically, I want to make sure an input contains ONLY digits and no other characters. Since +100 and -5 are both numbers, isNaN() is not the right way to go.
Perhaps a regexp is what I need? Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

夜唯美灬不弃 2024-08-19 06:08:56

如果您甚至想支持浮点值(点分隔值),那么您可以使用以下表达式:

var isNumber = /^\d+\.\d+$/.test(value);

If you want to even support for float values (Dot separated values) then you can use this expression :

var isNumber = /^\d+\.\d+$/.test(value);
不即不离 2024-08-19 06:08:56

这是另一种有趣、可读的方法来检查字符串是否只包含数字。

此方法的工作原理是使用 spread 将字符串拆分为数组运算符,然后使用 < code>every() 方法测试数组中的所有元素(字符)是否包含在数字字符串'0123456789'中:

const digits_only = string => [...string].every(c => '0123456789'.includes(c));

console.log(digits_only('123')); // true
console.log(digits_only('+123')); // false
console.log(digits_only('-123')); // false
console.log(digits_only('123.')); // false
console.log(digits_only('.123')); // false
console.log(digits_only('123.0')); // false
console.log(digits_only('0.123')); // false
console.log(digits_only('Hello, world!')); // false

Here's another interesting, readable way to check if a string contains only digits.

This method works by splitting the string into an array using the spread operator, and then uses the every() method to test whether all elements (characters) in the array are included in the string of digits '0123456789':

const digits_only = string => [...string].every(c => '0123456789'.includes(c));

console.log(digits_only('123')); // true
console.log(digits_only('+123')); // false
console.log(digits_only('-123')); // false
console.log(digits_only('123.')); // false
console.log(digits_only('.123')); // false
console.log(digits_only('123.0')); // false
console.log(digits_only('0.123')); // false
console.log(digits_only('Hello, world!')); // false

冰之心 2024-08-19 06:08:56

这是不使用正则表达式的解决方案:

function onlyDigits(s) {
  for (let i = s.length - 1; i >= 0; i--) {
    const d = s.charCodeAt(i);
    if (d < 48 || d > 57) return false
  }
  return true
}

其中 48 和 57 分别是“0”和“9”的字符代码。

Here is a solution without using regular expressions:

function onlyDigits(s) {
  for (let i = s.length - 1; i >= 0; i--) {
    const d = s.charCodeAt(i);
    if (d < 48 || d > 57) return false
  }
  return true
}

where 48 and 57 are the char codes for "0" and "9", respectively.

段念尘 2024-08-19 06:08:56

这就是你想要的

function isANumber(str){
  return !/\D/.test(str);
}

This is what you want

function isANumber(str){
  return !/\D/.test(str);
}
如梦初醒的夏天 2024-08-19 06:08:56

如果您需要在同一验证中使用整数和浮点

/^\d+\.\d+$|^\d+$/.test(val)

in case you need integer and float at same validation

/^\d+\.\d+$|^\d+$/.test(val)

囍笑 2024-08-19 06:08:56
function isNumeric(x) {
    return parseFloat(x).toString() === x.toString();
}

不过,对于带有前导零或尾随零的字符串,这将返回 false

function isNumeric(x) {
    return parseFloat(x).toString() === x.toString();
}

Though this will return false on strings with leading or trailing zeroes.

抠脚大汉 2024-08-19 06:08:56

那么,您可以使用以下正则表达式:

^\d+$

Well, you can use the following regex:

^\d+$
牵强ㄟ 2024-08-19 06:08:56

如果您想包含浮点值,也可以使用以下代码,

theValue=$('#balanceinput').val();
var isnum1 = /^\d*\.?\d+$/.test(theValue);
var isnum2 =  /^\d*\.?\d+$/.test(theValue.split("").reverse().join(""));
alert(isnum1+' '+isnum2);

这将仅测试数字和以“.”分隔的数字。第一个测试将涵盖 0.1 和 0 等值,但也涵盖 .1 ,
它不允许 0。所以我建议的解决方案是反转 theValue,这样 .1 将是 1。那么相同的正则表达式将不允许它。

例子 :

 theValue=3.4; //isnum1=true , isnum2=true 
theValue=.4; //isnum1=true , isnum2=false 
theValue=3.; //isnum1=flase , isnum2=true 

if you want to include float values also you can use the following code

theValue=$('#balanceinput').val();
var isnum1 = /^\d*\.?\d+$/.test(theValue);
var isnum2 =  /^\d*\.?\d+$/.test(theValue.split("").reverse().join(""));
alert(isnum1+' '+isnum2);

this will test for only digits and digits separated with '.' the first test will cover values such as 0.1 and 0 but also .1 ,
it will not allow 0. so the solution that I propose is to reverse theValue so .1 will be 1. then the same regular expression will not allow it .

example :

 theValue=3.4; //isnum1=true , isnum2=true 
theValue=.4; //isnum1=true , isnum2=false 
theValue=3.; //isnum1=flase , isnum2=true 
心病无药医 2024-08-19 06:08:56

如果您想为 . 留出空间,您可以尝试以下正则表达式。

/[^0-9.]/g

If you want to leave room for . you can try the below regex.

/[^0-9.]/g
無處可尋 2024-08-19 06:08:56

如果您使用 jQuery:

$.isNumeric('1234'); // true
$.isNumeric('1ab4'); // false

If you use jQuery:

$.isNumeric('1234'); // true
$.isNumeric('1ab4'); // false
〆一缕阳光ご 2024-08-19 06:08:56

这有效:

let output=Number.isInteger( Number(input) );

if(output) console.log('Input string contains only numbers');
else console.log('Input string contains does not contain only numbers');

This works:

let output=Number.isInteger( Number(input) );

if(output) console.log('Input string contains only numbers');
else console.log('Input string contains does not contain only numbers');
本宫微胖 2024-08-19 06:08:56

这是不使用正则表达式的解决方案

const  isdigit=(value)=>{
    const val=Number(value)?true:false
    console.log(val);
    return val
}

isdigit("10")//true
isdigit("any String")//false

Here's a Solution without using regex

const  isdigit=(value)=>{
    const val=Number(value)?true:false
    console.log(val);
    return val
}

isdigit("10")//true
isdigit("any String")//false
溺ぐ爱和你が 2024-08-19 06:08:56
c="123".match(/\D/) == null #true
c="a12".match(/\D/) == null #false

如果字符串仅包含数字,则返回 null

c="123".match(/\D/) == null #true
c="a12".match(/\D/) == null #false

If a string contains only digits it will return null

关于从前 2024-08-19 06:08:55

怎么样

let isnum = /^\d+$/.test(val);

how about

let isnum = /^\d+$/.test(val);
再浓的妆也掩不了殇 2024-08-19 06:08:55
string.match(/^[0-9]+$/) != null;
string.match(/^[0-9]+$/) != null;
誰ツ都不明白 2024-08-19 06:08:55
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文