English 中文(简体)
Check if string contains only digits
原标题:

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?

最佳回答

how about

let isnum = /^d+$/.test(val);
问题回答
string.match(/^[0-9]+$/) != null;
String.prototype.isNumber = function(){return /^d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false

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

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

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

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.

This is what you want

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

in case you need integer and float at same validation

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

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

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

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 

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

If you use jQuery:

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

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

/[^0-9.]/g
c="123".match(/D/) == null #true
c="a12".match(/D/) == null #false

If a string contains only digits it will return null





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签