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 wanna 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?
This question is related to
javascript
numbers
digits
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
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.
const isdigit=(value)=>{
const val=Number(value)?true:false
console.log(val);
return val
}
isdigit("10")//true
isdigit("any String")//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));_x000D_
_x000D_
console.log(digits_only('123')); // true_x000D_
console.log(digits_only('+123')); // false_x000D_
console.log(digits_only('-123')); // false_x000D_
console.log(digits_only('123.')); // false_x000D_
console.log(digits_only('.123')); // false_x000D_
console.log(digits_only('123.0')); // false_x000D_
console.log(digits_only('0.123')); // false_x000D_
console.log(digits_only('Hello, world!')); // false
_x000D_
This is what you want
function isANumber(str){
return !/\D/.test(str);
}
c="123".match(/\D/) == null #true
c="a12".match(/\D/) == null #false
If a string contains only digits it will return null
it checks valid number integers or float or double not a string
regex that i used
simple regex
1. ^[0-9]*[.]?[0-9]*$
advance regex
2. ^-?[\d.]+(?:e-?\d+)?$
eg:only numbers
var str='1232323';
var reg=/^[0-9]*[.]?[0-9]*$/;
console.log(reg.test(str))
_x000D_
eg:123434
eg:.1232323
eg:12.3434434
eg:1122212.efsffasf
eg:2323fdf34434
eg:0.3232rf3333
////////////////////////////////////////////////////////////////////
^-?[\d.]+(?:e-?\d+)?$
eg:13123123
eg:12344.3232
eg:2323323e4
eg:0.232332
function isNumeric(x) {
return parseFloat(x).toString() === x.toString();
}
Though this will return false
on strings with leading or trailing zeroes.
If you want to even support for float values (Dot separated values) then you can use this expression :
var isNumber = /^\d+\.\d+$/.test(value);
var str='1232323a.1';
var reg=/^[0-9]*[.]?[0-9]*$/;
console.log(reg.test(str))
_x000D_
in case you need integer and float at same validation
/^\d+\.\d+$|^\d+$/.test(val)
Well, you can use the following regex:
^\d+$
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
If you use jQuery:
$.isNumeric('1234'); // true
$.isNumeric('1ab4'); // false
string.match(/^[0-9]+$/) != null;
Source: Stackoverflow.com