How do you check if a string contains only numbers javascript?

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?

Jason Aller

3,48128 gold badges40 silver badges37 bronze badges

asked Nov 22, 2009 at 15:24

0

how about

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

Syntle

5,0783 gold badges12 silver badges34 bronze badges

answered Nov 22, 2009 at 15:26

Scott EverndenScott Evernden

37.8k14 gold badges78 silver badges84 bronze badges

9

string.match(/^[0-9]+$/) != null;

answered Nov 22, 2009 at 15:26

Jason SJason S

180k161 gold badges584 silver badges945 bronze badges

String.prototype.isNumber = function(){return /^\d+$/.test(this);} console.log("123123".isNumber()); // outputs true console.log("+12".isNumber()); // outputs false

answered Nov 22, 2009 at 21:25

baluptonbalupton

45.1k30 gold badges123 silver badges175 bronze badges

2

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

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

answered Jul 24, 2017 at 10:06

Adithya SaiAdithya Sai

1,4042 gold badges17 silver badges31 bronze badges

5

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

answered Nov 21, 2018 at 23:48

Grant MillerGrant Miller

25.3k16 gold badges137 silver badges152 bronze badges

3

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.

answered Jun 28, 2018 at 14:11

SC1000SC1000

73010 silver badges15 bronze badges

This is what you want

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

answered Dec 18, 2016 at 7:11

1

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

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

answered Jul 18, 2018 at 20:32

in case you need integer and float at same validation

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

answered Aug 5, 2020 at 13:50

1

Well, you can use the following regex:

^\d+$

answered Nov 22, 2009 at 15:26

JoeyJoey

334k81 gold badges672 silver badges672 bronze badges

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

answered Jun 9, 2020 at 15:25

1

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

answered Oct 24, 2020 at 8:57

3

If you use jQuery:

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

answered Jul 3, 2020 at 11:44

JacobJacob

3,3922 gold badges34 silver badges52 bronze badges

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

If a string contains only digits it will return null

answered Jul 25, 2019 at 9:04

How do you check if a string contains only numbers in JS?

Use the test() method to check if a string contains only digits, e.g. /^[0-9]+$/. test(str) . The test method will return true if the string contains only digits and false otherwise.

How do you check if a string contains numbers?

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.

How do you check if a string contains only letters and numbers in JavaScript?

Use the test() method on the following regular expression to check if a string contains only letters and numbers - /^[A-Za-z0-9]*$/ . The test method will return true if the regular expression is matched in the string and false otherwise. Copied!

How do I check if a string has letters and numbers?

The RegExp test() Method To check if a string contains only letters and numbers in JavaScript, call the test() method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .

Chủ đề